java Math常用方法

本文提供了多个数学函数的应用实例,包括三角函数、指数与对数、幂运算等,并演示了如何进行数值运算、取整及余数计算等操作。

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

//求sin值

double sin = Math.sin(3.14);

System.out.println(“sin3.14=” + sin);

//求cos值

double cos = Math.cos(0);

System.out.println(“cos0=” + cos);

//求tan值

double tan = Math.tan(0.785);

System.out.println(“tan0.785=” + tan);

//求arcsin

double arcsin = Math.asin(1);

System.out.println("arcsin1 = " + arcsin);

//求arccos

double arccos = Math.acos(1);

System.out.println("arccos = " + arccos);

//求arctan

double arctan = Math.atan(30);

System.out.println("arctan30 = " + arctan);

//求弧度

double radians = Math.toRadians(180);

System.out.println(“180度角的弧度为” + radians);

//求角度

double angle = Math.toDegrees(3.141592653589793);

System.out.println(“π的角度数为” + angle);

//求以e为底的指数

double exp = Math.exp(1);

System.out.println(“以e为底指数为1的数为” + exp);

//求以e为底e的平方的对数

double log = Math.log(Math.E * Math.E);

System.out.println(“以e为底e的平方的对数” + log);

//求以10为底100的对数

double log10 = Math.log10(100);

System.out.println(“以10为底100的对数” + log10);

//求100的平方根

double sqrt = Math.sqrt(100);

System.out.println(“100的平方根是” + sqrt);

//求27的立方根

double cbrt = Math.cbrt(27);

System.out.println(“27的立方根是” + cbrt);

//求10除以3的余数

double rest = Math.IEEEremainder(10, 3);

System.out.println(“10除以3的余数为” + rest);

//求0.9向上取整

double ceil = Math.ceil(0.9);

System.out.println(“0.9向上取整” + ceil);

//求2.49向下取整

double floor = Math.floor(2.49);

System.out.println(“2.49向下取整” + floor);

//求最接近参数的整数值(若有两个满足条件的数据则取为偶数的数据)

double rint = Math.rint(3.5);

System.out.println(“最接近参数的整数值” + rint);

//获得(1,1)坐标与x轴夹角度数

double atan2 = Math.atan2(1, 1);

System.out.println(“坐标(1,1)的极坐标为” + atan2);

//求3的5次方

double pow = Math.pow(3, 5);

System.out.println(“3的5次方” + pow);

//4舍5入

double round = Math.round(3.5);

System.out.println(“3.5四舍五入为” + round);

//计算2<<5+3<<4(会抛出结果溢出异常)------- 2<<5:2乘以2的5次方 3<<4:3乘以2的4次方

int sum = Math.addExact(2 << 5, 3 << 4);

System.out.println(“2<<5+3<<4=” + sum);

//计算2<<5-3<<4(会抛出结果溢出异常)

int subTract = Math.subtractExact(2 << 5, 3 << 4);

System.out.println(“2<<5-3<<4=” + subTract);

//计算2<<5*3<<4

int multiply = Math.multiplyExact(2 << 5, 3 << 4);

System.out.println(“2<<5*3<<4=” + multiply);

//计算2<<5加1(会跑出溢出异常)

int increment = Math.incrementExact(2 << 5);

System.out.println("2<<5+1 = " + increment);

//计算2<<5减1(会跑出溢出异常)

int decrementExact = Math.decrementExact(2 << 5);

System.out.println("2<<5-1 = " + decrementExact);

//计算2<<5的相反数

int negate = Math.negateExact(2 << 5);

System.out.println(“2<<5的相反数是” + negate);

//long转int 2<<15

int intNum = Math.toIntExact(2 << 15L);

System.out.println(intNum);

//2&3 2在2进制中为10 3为11 & 表示相同位都为1则为1,其他为0 10&11 2进制第二位都为1 所以 2&3=10=2

//multiplyHigh意义未知

//计算2<<5除以2<<4(取整)

int floorDiv = Math.floorDiv(2<<5,2<<4);

System.out.println(“2<<5/2<<4=”+floorDiv);

//取模运算(算法与取余相同,但负数运算是计算方式不同)

int floorMod = Math.floorMod(10,3);

System.out.println(“10/3的模=”+floorMod);

//取-52的绝对值

int abs = Math.abs(-52);

System.out.println("-52的绝对值="+abs);

//比较2<<5和3<<4的大小

int max = Math.max(2<<5,3<<4);

System.out.println(“2<<5与3<<4相比,较大的数为”+max);

//比较2<<5和3<<4的大小

int min = Math.min(2<<5,3<<4);

System.out.println(“2<<5与3<<4相比,较小的数为”+min);

//计算3乘5加4

double fma = Math.fma(3,5,4);

System.out.println(“3乘5加4=”+fma);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酸甜lemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值