Java logarithmic functions and Java logarithm operations

Source: Internet
Author: User
Tags base 10 logarithm natural logarithm

Java logarithmic functions and Java logarithm operations2010-05-17 10:32 China it lab anonymous Keyword: Java

Java logarithmic functions are computationally problematic, but there are amazing errors in the API. But if you use the following method, the Java processing of the number of small problems encountered can be easily solved.

Sun's J2SE provides a single Java logarithm method--double Java.lang.Math.log (double), which is easy to use. Take a look at the following code:

Double x = Math.log (5);

Equivalent to: x = ln 5 or x = Loge5, which is the natural logarithm of base e.

If you want to use Java to calculate the logarithm of the computer, how to do the different logarithm of the bottom? Unfortunately, we have not yet been able to calculate the logarithm of base 10 or base 2. But they are the most used when calculating the Java logarithm. To solve this problem, you need to use mathematical and logarithmic equations:

LOGX (y) =loge (x)/Loge (y), bottom-changing formula

This requires a simple Java program to implement the logarithmic operation:

Package Com.generationjava.math;

public class Logarithm {

static public double log (double value, double base) {

return Math.log (value)/Math.log (base);

}

}

Using the Java logarithmic function to calculate the base 10 logarithm of 100 becomes very simple:

Double log = Logarithm.log (100, 10); Log is 2.0

The base 2 logarithm of 512 is:

Double log = Logarithm.log (512, 2); Log is 9.0

The following two simple Java logarithm operations are also useful:

static public double log2 (double value) {

Return log (value, 2.0);

}

static public double log10 (double value) {

Return log (value, 10.0);

Java logarithmic functions and Java logarithm operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.