sqrt函数
想要计算一个数的平方根,可以使用math库中的sqrt函数
示例:
int a = (int)math.sqrt(4);
Math库 幂函数
Java中没有幂运算,因此要借助math库中的pow()函数进行幂运算
示例:
int a = pow(2,3);
floorMod函数
floorMod方法会限制表达式运算结果的范围,但是遗憾的是,floorMod对于负除数,依然会得到负数结果,不过这种情况在实际中很少出现。
示例:
floorMod(10/2,12);
这个式子,总是会得到0~12之间的数
math库三角函数
math库提供里一些常用的三角函数
Math.sin
Math.cos
Math.tan
Math.atan
Math.atan2
math库指数函数与反函数
math库中提供指数函数以及它的反函数
Math.exp
Math.log
Math.log10
pi和e的近似值
Java提供里两个用于表示pi和e的值
Math.PI
Math.E
程序示例
public class FirstCode1 {
public static void main(String[] args) {
//sqrt()函数
int a = (int)Math.sqrt(4);
System.out.println("a ="+a);
//pow()函数
int b = (int) Math.pow(3,4);
System.out.println("b = "+b);
//fllorMod()函数
int c = (int) Math.floorMod(a+b,12);
System.out.println("c = "+c);
//math库中的三角函数
double d1 = Math.sin(a);
double d2 = Math.cos(a);
double d3 = Math.tan(a);
double d4 = Math.atan(a);
double d5 = (double) Math.atan2(2,3);
System.out.println("d1 = "+d1);
System.out.println("d2 = "+d2);
System.out.println("d3 = "+d3);
System.out.println("d4 = "+d4);
System.out.println("d5 = "+d5);
//指数函数及其反函数
double e1 = Math.exp(2);//返回自然数底数 e 的参数次方
double e2 = Math.log(2);
double e3 = Math.log10(2);
System.out.println("e1 = "+e1);
System.out.println("e2 = "+e2);
System.out.println("e3 = "+e3);
//pi与e常量的值
double f1 = Math.PI;
double f2 = Math.E;
System.out.println("f1 = "+f1);
System.out.println("f2 = "+f2);
}
}
运行结果
a =2
b = 81
c = 11
d1 = 0.9092974268256817
d2 = -0.4161468365471424
d3 = -2.185039863261519
d4 = 1.1071487177940904
d5 = 0.5880026035475675
e1 = 7.38905609893065
e2 = 0.6931471805599453
e3 = 0.3010299956639812
f1 = 3.141592653589793
f2 = 2.718281828459045
StricMath库
StricMath库实现了“可完全分发的数学库”,确保在所有平台上得到相同的结果
注意: