在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数、对数、平方根和三角函数等。Math 类位于 java.lang 包,它的构造方法是 private 的,因此无法创建 Math 类的对象,并且 Math 类中的所有方法都是类方法,可以直接通过类名来调用它们。
代码测试
import org.junit.Test; import java.util.Random; public class TestClass { /** *返回参数的绝对值。 * */ @Test public void test1(){ Integer a = -8; double d = -100; float f = -90; System.out.println(Math.abs(a)); System.out.println(Math.abs(d)); System.out.println(Math.abs(f)); } /** *返回一个最接近的int、long型值,方法会指定返回的数据类型。四舍五入 * */ @Test public void test2(){ double d = 100.675; double e = 100.441; float f = 100; float g = 90f; System.out.println(Math.round(d)); System.out.println(Math.round(e)); System.out.println(Math.round(f)); System.out.println(Math.round(g)); } /** *该方法返回 double 值。 * */ @Test public void test3(){ System.out.println( Math.random() ); System.out.println( Math.random() ); Random rand=new Random(); int i=(int)(Math.random()*100); // 生成0-100的随机数 int j=rand.nextInt(100); // 这里是一个方法的重载,参数的内容是指定范围 System.out.println("i:"+i+"\nj:"+j); // 分别输出两个随机数 } /** *返回第一个参数的第二个参数次方。 * */ @Test public void test4(){ double x = 11.635; double y = 2.76; System.out.printf("e 的值为 %.4f%n", Math.E); System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y)); } /** *返回 double 类型,返回值大于或等于给定的参数。 * */ @Test public void test5(){ double d = 100.675; float f = -90; System.out.println(Math.ceil(d)); System.out.println(Math.ceil(f)); System.out.println(Math.floor(d)); System.out.println(Math.floor(f)); } /** *返回参数的算术平方根。 * */ @Test public void test6(){ double x = 11.635; double y = 2.76; System.out.printf("e 的值为 %.4f%n", Math.E); System.out.printf("sqrt(%.3f) 为 %.3f%n", x, Math.sqrt(x)); } /** *返回参数的自然数底数的对数值。 * */ @Test public void test7(){ double x = 11.635; double y = 2.76; System.out.printf("e 的值为 %.4f%n", Math.E); System.out.printf("log(%.3f) 为 %.3f%n", x, Math.log(x)); } /** *返回 double 类型数组,小于或等于给定的参数。 * */ @Test public void test8(){ double d = 100.675; float f = -90; System.out.println(Math.floor(d)); System.out.println(Math.floor(f)); System.out.println(Math.ceil(d)); System.out.println(Math.ceil(f)); } /** *返回以度为单位的角度 * */ @Test public void test9(){ System.out.println(Math.toDegrees(Math.PI/2)); } /** *以弧度表示的角度 * */ @Test public void test10(){ System.out.println(Math.toRadians(30)); } /** *返回余弦值,-1.0到1.0的值 * */ @Test public void test11(){ double x = 45.0; //将角度转换成弧度值 x = Math.toRadians(x); //180度 double y = 3.141592653589793; System.out.println(Math.cos(x)); System.out.println(Math.cos(y)); } /** *角度值[0~π] * */ @Test public void test12(){ double x = 0.32; System.out.println(Math.acos(x)); } /** *返回正弦值,-1.0到1.0的值 * */ @Test public void test13(){ double a = Math.toRadians(90);//把数字90转换成90度 System.out.println(Math.sin(a));//计算sin 90度 } /** *返回弧度值,角度范围在 -pi/2 到 pi/2 之间,即-180度 到 180度 * */ @Test public void test14(){ double x = 0.32; System.out.println(Math.asin(x)); } /** *弧正切 * */ @Test public void test15(){ double tan = Math.tan(Math.PI * 45 / 180); // 求角度的tan double aa = Math.atan(1) * 180 / Math.PI;// 根据tan求角度 System.out.println(tan); System.out.println(aa); } /** *返回弧正切的值,返回的角度范围在 -pi/2 到 pi/2 之间。 * */ @Test public void test16(){ double aa = Math.atan(1) * 180 / Math.PI;// 根据tan求角度 System.out.println(aa); } }