JAVA之Math类的数学运算应用详解

 Math类中定义了许多方法,这些方法都被定义为static形式,经过Math类能够在主函数中直接调用java

调用方法:app

Math.方法;函数

Math类中还定义了一些数学常量如:PI,E;spa

调用方法:.net

Math.PI;(PI表示π,即平角)blog

Math.E;ip

Math类方法:get

1.三角函数方法:数学

double  sin(double a ) : 返回角的三角正弦flash

double cos(double a)  : 返回角的三角余弦

double tan(double  a)  : 返回角的三角正切

double asin(double a) : 返回角的反正弦

double acos(double a)  : 返回角的反余弦

double atan(double a)  : 返回角的反正切

double toRadians(double a) : 将角转换为弧度

doueble toDegrees(double a) : 将弧度转化为角

注意:

以上方法除了toRadians()外,参数均为double型,即以弧度代替角度来实现;

而toRadians()则以角度为参数。

eg:

 

[java] view plain copy

  1. package Number;  
  2. public class IntFunction {  
  3.     public static void main (String []args)  
  4.     {  
  5.         System.out.println("90度的正弦值:" + Math.sin(Math.PI/2));  
  6.         System.out.println("0度的余弦值:" + Math.cos(0));  
  7.         System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  
  8.         System.out.println("2的平方根与2商的反正弦值: " + Math.asin(Math.sqrt(2)/2));  
  9.         System.out.println("2的平方根与2商的反余弦值: " + Math.acos(Math.sqrt(2)/2));  
  10.         System.out.println("1的反正切值: " + Math.atan(1));  
  11.         System.out.println("120度的弧度值:" + Math.toRadians(120));  
  12.         System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  
  13.         System.out.println(Math.PI);  
  14.     }  
  15. }  

2.指数函数方法:

 

double exp(double a) : 用于获取e的a次方;

double log(double a) : 即lna;
double log10(double a) : 即log10a;

double sqrt(double a ):用于取a的平方根;

double cbrt(double a) : 用于取a的立方根;

double pow(double a, double b) : 用于求a的b次方;

eg:

 

[java] view plain copy

  1. package Number;  
  2. public class IntFunction {  
  3.     public static void main (String []args)  
  4.     {  
  5.         System.out.println("e的平方值: " + Math.exp(2));  
  6.         System.out.println("以e为底2的对数值:" + Math.log(2));  
  7.         System.out.println("以10为底2的对数值:" + Math.log10(2));  
  8.         System.out.println("4的平方根值:" + Math.sqrt(4));  
  9.         System.out.println("8的立方根值: " + Math.cbrt(8));  
  10.         System.out.println("2的2次方值: " + Math.pow(2, 2));  
  11.     }  
  12. }  


3.取整函数方法:

 

double ceil(double a):返回大于等于a的整数值,返回值类型为double;

double floor(double a) : 返回小于等于a的整数值,返回值类型为double;

double rint(double a) : 返回与a最接近的整数值,返回值类型为double;(若是两个同为整数且一样接近,选取偶数值的那个)

int round(double a ): 其值等于Math.floor(a + 0.5),返回值类型为long;

long round(float a ): 其值等于Math.floor(a + 0.5),返回值类型为int;

eg:

 

[java] view plain copy

  1. package Number;  
  2. public class IntFunction {  
  3.     public static void main (String []args)  
  4.     {  
  5.         System.out.println("使用ceil()方法取整: " + Math.ceil(5.2));//6.0  
  6.         System.out.println("使用floor()方法取整" + Math.floor(2.5));//2.0  
  7.         System.out.println("使用rint()方法取整: " + Math.rint(2.7));//3.0  
  8.         System.out.println("使用rint()方法取整: " + Math.rint(2.5));//2.0  
  9.         System.out.println("使用round()方法取整: " + Math.round(3.4f));//3  
  10.         System.out.println("使用round()方法取整: " + Math.round(2.5));//3  
  11.         System.out.println("使用round()方法取整: " + Math.round(-2.5));//-2  
  12.         System.out.println("使用round()方法取整: " + Math.round(-4.3));//-4  
  13.     }  
  14. }  
  15. /**输出结果: 
  16. *使用ceil()方法取整: 6.0 
  17. *使用floor()方法取整2.0 
  18. *使用rint()方法取整: 3.0 
  19. *使用rint()方法取整: 2.0 
  20. *使用round()方法取整: 3 
  21. *使用round()方法取整: 3 
  22. *使用round()方法取整: -2 
  23. *使用round()方法取整: -4 
  24. */  
相关文章
相关标签/搜索