博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.Math类方法
阅读量:3968 次
发布时间:2019-05-24

本文共 1067 字,大约阅读时间需要 3 分钟。

java.lang.Math类方法

1.Math.max 求两数中最大/Math.min 求两数中最小
package Test0228;
public class Test007 {public static void main(String[] args) {
double x=12.45,y=52.4; System.out.println(Math.max(x, y)); System.out.println(x<y?x:y);}}
2.Math.round 四舍五入返回int型或者long型
package Test0228;
public class Test008 { public static void main(String[] args) {
double x = 12.56;
double y= 125.69;
double w=12.1; System.out.println(Math.round(x)); System.out.println(Math.round(y)); System.out.println(Math.round(w));
}}
3.Math.abs 求绝对值,典型使用场景就是浮点
数的等值判定问题
package Test0228;
public class Test0001 {public static void main(String[] args) {
double res = 0;
for (int i=0;i<10;i++) {
res+=0.1;
}
if(Math.abs(1-res)<1e-6) { System.out.println(“计算结果为:”);
}else
System.out.println(“结果为:”+res);}}
1e-6,其中e可以大写,也可以小写,这是浮点数中的科学计数法,表示1*10-6
三角函数
Math.sin 正弦函数 Math.asin 反正弦函数Math.cos 余弦函数 Math.acos 反余弦函数Math.tan 正切函数 Math.atan 反正切函数Math.toDegrees 弧度转化为角度 Math.toRadians 角度转化为弧度
指数计算
Math.sqrt 求开方 Math.sqrt(4)Math.pow 求某数的任意次方, 抛出ArithmeticException处理溢出异常Math.exp 求e的任意次方Math.log10 以10为底的对数Math.log 自然对数

转载地址:http://bgcki.baihongyu.com/

你可能感兴趣的文章
UTF-16 编码简介
查看>>
Java 变量名
查看>>
Java 四舍五入运算
查看>>
Spring Batch 例子: 运行系统命令
查看>>
Spring Batch 核心概念
查看>>
Spring Batch 例子: 导入定长文件到数据库
查看>>
正则表达式
查看>>
Java I/O
查看>>
序列化
查看>>
Perl 精萃
查看>>
Perl 简介
查看>>
Perl 注释
查看>>
数据类型之标量
查看>>
调试 Perl 脚本
查看>>
增强的for循环语句
查看>>
静态导入
查看>>
java 泛型
查看>>
控制结构
查看>>
标准输入输出
查看>>
运算符
查看>>