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

本文详细介绍了Java中的Math类,包括三角函数、指数函数、取整函数等常用方法及其使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

调用方法:

Math.方法;

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

调用方法:

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

Math.E;

Math类方法:

1.三角函数方法:

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

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. */  

转载于:https://my.oschina.net/vshcxl/blog/730741

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值