Math位于java.lang包下,当我们试图调用Math.sin方法时候,在Math类中显示的是StrictMath中的 sin方法
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}
StrictMath中仅仅定义了方法,并且申明为static native
/**
* Returns the trigonometric sine of an angle. Special cases:
* <ul><li>If the argument is NaN or an infinity, then the
* result is NaN.
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.</ul>
*
* @param a an angle, in radians.
* @return the sine of the argument.
*/
public static native double sin(double a);
在Math类中,为了达到最快的性能,所有的方法都使用计算机浮点单元中的历程。如果得到一个完全可预测的结果比运行速度更重要的话,就应该使用StrictMath类。它使用“自由发布的Math库”实现算法,以去报在所有平台上得到相同的结果。有关这些算法的源代码请参阅 http://www.netlib.org/fdlibm/index.html
参考:
Math类还提供指数函数以及它的反函数--自然对数 Math.exp Math.log
Math类还提供了两个用于表示π和e常量的近似值 Math.PI Math.E
提示:从JDK5.0开始,不必在数学方法和常量名前添加前缀Math,而只用在源文件的顶部加上下列内容就可以 Import static java.lang.Math.*;
System.out.println("The square root of \u03C0 is "+sqrt(PI));