黑马程序员____Mah类

----------------------android培训java培训、期待与您交流! ----------------------

Java 语言中的Mah类定义了数学常量和一些常用的数学函数,它们位于java.util包中,其定义如下所示:

public final Math
{
	public static final double E=2.7182818284590452354;
	public static final double PI=3.1415926535879323846;
	public static double abs(double x); //绝对值
	public static native double atan(double x);//反正切值
	public static native double ceil(double x);//天花板值
	public static native double cos(double x);
	public static native double exp(double x);//e 为底的指数值
	public static native double floor(double x);
	public static native double log(double x);//e为底的对数
	public static native double max(double x,double y);
	public static native double min(double x,double y);
	public static native double pow(double x,double y);//幂指数
	public static native synchronized double random();
	public static long round(double x);
	public static native double sin(double x);
	public static native double sqrt(double x);//平方根
	public static native double tan(double x);
}

请注意,这些成员均是static的,也就意味着可以用“Math.”前缀的方式来对他们进行调用,而不要使用Math类的某个对象。
例1 测试Math类

import java.util.*;
public class Ex0110
{
	public static void main(String[] args)
	{
		final double PI=Math.PI;
		final double E=Math.E;
		System.out.println("E			="+E);
		System.out.println("Math.exp(1.0)		="+Math.exp(1.0));
		System.out.println("PI			="+PI);
		System.out.println("4*Math.atan(1.0)	="+Math.atan(1.0));
		System.out.println("Math.cos(2*PI)		="+Math.cos(2*PI));
		System.out.println("Math.sin(PI/2)		="+Math.sin(PI/2));
		System.out.println("Math.tan(PI/4)		="+Math.tan(PI/4));
		System.out.println("Math.log(E)		="+Math.log(E));
		System.out.println("Math.abs(-13.579)	="+Math.abs(-13.579));
		System.out.println("Math.floor(13.579)	="+Math.floor(13.579));
		System.out.println("Math.ceil(13.579)	="+Math.ceil(13.579));
		System.out.println("Math.round(13.579)	="+Math.round(13.579));
		System.out.println("Math.pow(25.0,0.5)	="+Math.pow(25.0,0.5));
		System.out.println("Math.sqrt(25.0)		="+Math.sqrt(25.0));
		System.out.println("Math.random()		="+Math.random());
		System.out.println("Math.random()		="+Math.random());
	}
}

该程序的输出为:

注意,Math.tan(PI/4)方法在计算时的细微差错,其正确的指应当为1.0。同时应注意到Math.round(double)返回的值类型为long型整数而不是double型十进制浮点数,而Math类中其他方法的返回结果均是double型的。

Maht.random()方法随机产生一个在0.01.0之间均匀分布的双精度十进制浮点数,下面的程序将对这种分布进行测试。

2 测试Math.random()方法

下面的程序产生0.0到1.0间的50000个随机数,对区间0.0到1.0进行100等分,子区间的宽度为0.01,然后对落在各个区间的随机数个数进行统计,该程序使用frenquency[]数组来记录累加结果。例如,随机数x=0.7294416115902632落在区间0.72到0.73中,所以使frequency[72]加1。

public class Ex0111
{
	public static void main(String [] args)
	{
		final int SUBINTERVALS = 100;
		final int TOTAL = 50000;
		int [] frequency = new int[SUBINTERVALS];
		for(int k=0;k<SUBINTERVALS;k++)
		frequency[k] = 0;
		for(int j=0;j<TOTAL;j++)
		{
			double x =Math.random();	//0.0<x<1.0
			x *=SUBINTERVALS;			//0.0<X<100.0
			x = Math.floor(x);          //0.0<=x<=99.0
			long m = Math.round(x);		//0<=m<=99
			int k = (int)m;				//0<=m<=99	
			++frequency[k];		        //count k,对所在区间的数的个数进行计数
		}
		for(int i=0;i<SUBINTERVALS;i++)
		{
			System.out.print(frequency[i]+(i%10==9?"\n":"\t"));
		}
	}
}

该程序的输出结果为:


假如上述50000个随机数均匀地落在各个子区间中的话,则每个子区间中应该有500个。从上述程序的结果来看几乎是接近500的,这也就从实验的角度得到了验证。因此有理由相信Maht.random()方法确实能够产生分布均匀的随机数。

例题3 测试Math.sqrt()方法

下面的程序将对Math.sqrt()方法进行测试。在程序中,定义了一个局部求解平方根的方法,我们将Math.sqrt()所返回的值及该值得平方与该局部方法所获得的值进行比较:

public class Ex0112
{
	public static void main(String [] args)
	{
		for(double x =50.0;x<60.0;x++)
		{
			double y=Math.sqrt(x);
			double z=sqrt(x);
			System.out.println(y+"\t"+y*y);
			System.out.println(z);
		}
	}
	private static double sqrt(double x)
	{
		final double EPSILON=1E-14;
		if(x<=0) return 0.0;
		double y=1.0;
		while(Math.abs(y*y-x)>EPSILON)
			y = (y+x/y)/2;
		return y;
	}
}

该程序得输出结果为:

---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值