package AL.java;
public class MyMath {
public static int abs(int x) {
if(x < 0)
return -x;
else
return x;
}
//计算绝对值
public static double abs(double x) {
if(x < 0.0)
return -x;
else
return x;
}
//判断素数
public static boolean isPrime(int n) {
if(n < 2)
return false;//n<2,n不是素数
for(int i=2; i*i <= n;i++) {
if(n%i == 0)
return false;
}//i->[2,n) , i^x<=n , n%i != 0,n为素数
return true;
}
//计算平方根
public static double sqrt(double c) {
if(c<0)
return Double.NaN;
double err = 1e-15;
double t = c;
while(Math.abs(t-c/t) > err*t) {
t = (c/t +t)/2.0;
}
return t;
}
//计算直角斜边长
public static double hypotenuse(double a,double b) {
return sqrt(a*a + b*b);
}
}
Java 常用数学计算公式
最新推荐文章于 2024-06-07 10:36:50 发布