读书人

Math跟StrictMath

发布时间: 2013-01-26 13:47:01 作者: rapoo

Math和StrictMath

Math位于java.lang包下,当我们试图调用Math.sin方法时候,在Math类中显示的是StrictMath中的 sin方法

 /**     * 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));

读书人网 >编程

热点推荐