欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对数log ln lg 的java实现和mysql实现

程序员文章站 2024-02-27 19:00:33
...

一、基本概念

log: 表示对数,与指数相反。例如:
对数log ln lg 的java实现和mysql实现我们读作log以3为底,9的对数。具体计算方式是3的2次方为9,及以3为底9的对数就是2。
lg: 10为底的对数,叫作常用对数。
ln: 以无理数e(e=2.71828…)为底的对数,叫作自然对数

对数是对求幂的逆运算,正如除法是乘法的倒数,反之亦然。 这意味着一个数字的对数是必须产生另一个固定数字(基数)的指数。 在简单的情况下,乘数中的对数计数因子。更一般来说,乘幂允许将任何正实数提高到任何实际功率,总是产生正的结果,因此可以对于b不等于1的任何两个正实数b和x计算对数。

如果a的x次方等于N(a>0,且a不等于1),那么数x叫做以a为底N的对数(logarithm),记作x=logₐN。其中,a叫做对数的底数,N叫做真数。

二、javaAPI

1.求lg

java.lang.Math @Contract(pure = true) 
public static double log10(double a)

Returns the base 10 logarithm of a double value. 
返回以10为底的对数
Special cases:
If the argument is NaN or less than zero, then the result is NaN.
如果参数是NaN或者小于0的数据,则返回NaN
If the argument is positive infinity, then the result is positive infinity.
如果参数是正无群大,返回正无群大
If the argument is positive zero or negative zero, then the result is negative infinity.
如果参数为正零或负零,则结果为负无穷大。
If the argument is equal to 10n for integer n, then the result is n.
如果整数n的参数等于10n,则结果为n。
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.
计算结果必须在精确结果的1ulp范围内。结果必须是半单调的。

2.求ln

java.lang.Math @Contract(pure = true) 
public static double log(double a)

Returns the natural logarithm (base e) of a double value. 
返回double类型的自然对数(以e为底)。
Special cases:
If the argument is NaN or less than zero, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is positive zero or negative zero, then the result is negative infinity.
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Params:
a – a value
Returns:
the value ln a, the natural logarithm of a.
值ln a,a的自然对数。
External annotations:
@org.jetbrains.annotations.Contract(pure = true)

3.求ln(x+1)

java.lang.Math @Contract(pure = true) 
public static double log1p(double x)

Returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x).
返回参数和1之和的自然对数。注意,对于小值x,log1p(x)的结果比log(1.0+x)的浮点求值更接近ln(1+x)的真实结果。

Special cases:
If the argument is NaN or less than -1, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is negative one, then the result is negative infinity.
If the argument is zero, then the result is a zero with the same sign as the argument.
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Params:
x – a value
Returns:
the value ln(x + 1), the natural log of x + 1
 ln(x + 1),x+1的自然对数
Since:
1.5
External annotations:
@org.jetbrains.annotations.Contract(pure = true)

4.求任意底数的对数
实现原理:换底公式
对数log ln lg 的java实现和mysql实现

static public double log(double value, int base){
		return Math.log(value) / Math.log(base);
}

5.代码示例

public static void main(String[] args) {
		System.out.println("Math.log==>"+Math.log(-1));
		System.out.println("Double.NaN==>"+Double.NaN);
		System.out.println("Math.log10(10)==>"+Math.log10(10));
		System.out.println("Math.log1p(9)==>"+Math.log1p(9));
		System.out.println("Math.log(10)==>"+Math.log(10));
		System.out.println("log2 8==>"+log(8,2));
		System.out.println("log5 125==>"+log(125, 5));
	}

运行结果

Math.log==>NaN
Double.NaN==>NaN
Math.log10(10)==>1.0
Math.log1p(9)==>2.302585092994046
Math.log(10)==>2.302585092994046
log2 8==>3.0
log5 125==>3.0000000000000004

三、Mysql函数

Name Description
LN() Return the natural logarithm of the argument
LOG() Return the natural logarithm of the first argument
LOG10() Return the base-10 logarithm of the argument
LOG2() Return the base-2 logarithm of the argument

参考链接:https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_log

1.LN(X)

Returns the natural logarithm of X; that is, the base-e logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning “Invalid argument for logarithm” is reported.
返回X的自然对数,即X的e底对数。如果X小于或等于0.0E0,则函数返回空值,并报告警告“对数参数无效”

mysql> SELECT LN(2);
        -> 0.69314718055995
mysql> SELECT LN(-2);
        -> NULL
        
This function is synonymous with LOG(X). The inverse of this function is the EXP() function.
此函数与LOG(X)同义此函数的逆函数是EXP()函数。

2.LOG(X), LOG(B,X)

If called with one parameter, this function returns the natural logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning “Invalid argument for logarithm” is reported.
如果使用一个参数调用,则此函数返回X的自然对数。如果X小于或等于0.0E0,则此函数返回空值,并报告警告“对数参数无效”。
The inverse of this function (when called with a single argument) is the EXP() function.
此函数的逆函数(使用单个参数调用时)是EXP()函数。

mysql> SELECT LOG(2);
        -> 0.69314718055995
mysql> SELECT LOG(-2);
        -> NULL

If called with two parameters, this function returns the logarithm of X to the base B. If X is less than or equal to 0, or if B is less than or equal to 1, then NULL is returned.
如果使用两个参数调用,则此函数将X的对数返回到基数B。如果X小于或等于0,或者如果B小于或等于1,则返回NULL。

mysql> SELECT LOG(2,65536);
        -> 16
mysql> SELECT LOG(10,100);
        -> 2
mysql> SELECT LOG(1,100);
        -> NULL
       
LOG(B,X) is equivalent to LOG(X) / LOG(B).
LOG(B,X)相当于LOG(X)/LOG(B)。

3.LOG2(X)

Returns the base-2 logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning “Invalid argument for logarithm” is reported.
返回X的以2为底的对数。如果X小于或等于0.0E0,则函数返回空值,并报告警告“对数参数无效”。

mysql> SELECT LOG2(65536);
        -> 16
mysql> SELECT LOG2(-100);
        -> NULL
        
LOG2() is useful for finding out how many bits a number requires for storage. This function is equivalent to the expression LOG(X) / LOG(2).
LOG2()有助于找出一个数字存储需要多少位。此函数相当于表达式LOG(X)/LOG(2)。

4.LOG10(X)

Returns the base-10 logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning “Invalid argument for logarithm” is reported.
返回X的以10为底的对数。如果X小于或等于0.0E0,则函数返回空值,并报告警告“对数参数无效”。

mysql> SELECT LOG10(2);
        -> 0.30102999566398
mysql> SELECT LOG10(100);
        -> 2
mysql> SELECT LOG10(-100);
        -> NULL

LOG10(X) is equivalent to LOG(10,X).
LOG10(X)相当于LOG(10,X)

参考文档:
1.java8API
2.mysql官方文档:https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_log