码迷,mamicode.com
首页 > 其他好文 > 详细

Math与StrictMath

时间:2015-08-17 17:18:26      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:jdk   math   

我们编写程序,经常使用到jdk的Math类,然而,Math里面许多数学函数方法是直接调用的StrictMath类里面的方法,而这些函数方法在StrictMath里面的形式是调用本地的非Java代码的接口,使用native关键字进行修饰

Math中的部分代码(从java.lang.Math类中提取的部分代码)

package java.lang;

import java.util.Random;

import sun.misc.FloatConsts;
import sun.misc.DoubleConsts;

public final class Math {

	/**
	 * Don't let anyone instantiate this class.
	 */
	private Math() {
	}

	public static final double E = 2.7182818284590452354;
	public static final double PI = 3.14159265358979323846;

	public static double sin(double a) {
		return StrictMath.sin(a); // default impl. delegates to StrictMath
	}

	public static double cos(double a) {
		return StrictMath.cos(a); // default impl. delegates to StrictMath
	}

	public static double tan(double a) {
		return StrictMath.tan(a); // default impl. delegates to StrictMath
	}

	public static double asin(double a) {
		return StrictMath.asin(a); // default impl. delegates to StrictMath
	}

	public static double acos(double a) {
		return StrictMath.acos(a); // default impl. delegates to StrictMath
	}

	public static double atan(double a) {
		return StrictMath.atan(a); // default impl. delegates to StrictMath
	}

	public static double exp(double a) {
		return StrictMath.exp(a); // default impl. delegates to StrictMath
	}

	public static double log(double a) {
		return StrictMath.log(a); // default impl. delegates to StrictMath
	}

	public static double log10(double a) {
		return StrictMath.log10(a); // default impl. delegates to StrictMath
	}

	public static double sqrt(double a) {
		return StrictMath.sqrt(a); // default impl. delegates to StrictMath
									// Note that hardware sqrt instructions
									// frequently can be directly used by JITs
									// and should be much faster than doing
									// Math.sqrt in software.
	}

	public static double cbrt(double a) {
		return StrictMath.cbrt(a);
	}

	public static double atan2(double y, double x) {
		return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
	}

	public static double pow(double a, double b) {
		return StrictMath.pow(a, b); // default impl. delegates to StrictMath
	}

	public static double sinh(double x) {
		return StrictMath.sinh(x);
	}

	public static double cosh(double x) {
		return StrictMath.cosh(x);
	}

	public static double tanh(double x) {
		return StrictMath.tanh(x);
	}

	public static double hypot(double x, double y) {
		return StrictMath.hypot(x, y);
	}

	public static double expm1(double x) {
		return StrictMath.expm1(x);
	}

	public static double log1p(double x) {
		return StrictMath.log1p(x);
	}

}
StrictMath中的部分代码:

package java.lang;

import java.util.Random;
import sun.misc.DoubleConsts;

public final class StrictMath {

	/**
	 * Don't let anyone instantiate this class.
	 */
	private StrictMath() {
	}

	public static final double E = 2.7182818284590452354;

	public static final double PI = 3.14159265358979323846;

	public static native double sin(double a);

	public static native double cos(double a);

	public static native double tan(double a);

	public static native double asin(double a);

	public static native double acos(double a);

	public static native double atan(double a);

	public static native double exp(double a);

	public static native double log(double a);

	public static native double log10(double a);

	public static native double sqrt(double a);

	public static native double cbrt(double a);

	public static native double atan2(double y, double x);

	public static native double pow(double a, double b);

	public static native double sinh(double x);

	public static native double cosh(double x);

	public static native double tanh(double x);

	public static native double hypot(double x, double y);

	public static native double expm1(double x);

	public static native double log1p(double x);

}
根据上面的代码,可以看出,Math类的中的方法实现调用的StrictMath来实现,而在StrictMath类中方法用native修饰,表明调用的并非java代码,而是其它的,这里便是C代码来实现这些方法的,而这些源码在jdk中是没有公布的,根据注释可以获知这些

C的实现来之ftp://ftp.netlib.org/fdlibm.tar这里,但是却不能访问,于是百度netlib发现在http://www.netlib.org/liblist.html下的fdlibm中,使用C代码来实现这些基本的函数好处就是提高运行的效率


总结:

两个类的相同点:都是实现了基本的数学方法,都有互相调用的地方

不同点:两个类的所有相同方法中,Math类调用了StrictMath类的方法,而且都是用native修饰,C代码实现的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。

Math与StrictMath

标签:jdk   math   

原文地址:http://blog.csdn.net/lsx991947534/article/details/47727301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!