Math与StrictMath

我们编写程序,经常使用到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代码实现的方法

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值