Using log base 10 in a formula in Java

后端 未结 5 502
遥遥无期
遥遥无期 2021-01-07 16:18

I\'m trying to write a Java program that can take values and put them into a formula involving log base 10.

How can I calculate log10 in Java?

相关标签:
5条回答
  • 2021-01-07 16:57

    the below complete example may help you

    public class Log10Math {
        public static void main(String args[]) {
            // Method returns logarithm of number argument with base ten(10);
            short shortVal = 10;
            byte byteVal = 1;
            double doubleVal = Math.log10(byteVal);
    
            System.out.println("Log10 Results");
            System.out.println(Math.log10(1));
            System.out.println(doubleVal);
            System.out.println(Math.log10(shortVal));
        }
    }
    

    Output

    Log10 Results
    0.0
    0.0
    1.0
    
    0 讨论(0)
  • 2021-01-07 16:57

    in Java we can use log function as that. (int) Math.log(val); int is a type to convert it into this , in which you are storing ur answer.

    0 讨论(0)
  • 2021-01-07 17:04

    It looks like Java actually has a log10 function:

    Math.log10(x)
    

    Otherwise, just use math:

    Math.log(x) / Math.log(10)
    

    http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

    0 讨论(0)
  • 2021-01-07 17:18
    Math.log10(x)
    

    suffices for floating-point numbers.

    If you need integer log base 10, and you can use third-party libraries, Guava provides IntMath.log10(int, RoundingMode). (Disclosure: I contribute to Guava.)

    0 讨论(0)
  • 2021-01-07 17:20

    You can do this too

    class log
    {
        public void main(double a)
        {
            double l = 0;
            l = Math.log10(a);
            System.out.println(l);
        }
    }
    
    0 讨论(0)
提交回复
热议问题