Java中的Math类示例| Java Math Class教程

Java中的Math类示例| Java Math Class Tutorial是今天的主题。有几次我们需要在程序中执行一些数学函数。有时我们可以为它们创建单独的方法,但在制造大而复杂的问题时,我们没有时间这样做。因此,要在我们的程序中实现这些函数,我们可以使用已在Math类中定义的方法(它存在于java.lang.Math包中)。

内容概述

  • Java中的1个Math类
  • 2导入数学类
  • 3数学课的各种方法
  • 4推荐帖子

Java中的Math类

使用Math类,我们可以实现min(),max(),avg(),sin(),cos(),tan(),round(),ceil(),floor(),abs()等方法, pow(),floor(),ciel(),round()等

导入数学价格

如果我们想在java中导入Math类,我们必须编写以下代码。

java.lang.math 

数学课的各种方法

以下是可以使用Math类在程序代码中直接实现的方法。

方法 工作描述
Math.max() 它需要两个值作为输入,并返回两个值的最大值。
Math.min() 它需要两个值作为输入,并返回两个值的最小值。
Math.abs() 它将获取一个值作为输入并返回它的绝对值。
Math.round() 它用于将十进制数的四舍五入为最接近的整数值。
Math.sqrt() 它将一个参数作为输入并返回该数字的平方根。
Math.cbrt() 它将一个参数作为输入并返回该数字的多维数据集根。
Math.atan() 它用于返回给定值的三角Arc Tangent值
Math.hypot() 此方法将两个double值作为参数并返回sqrt(x2 + y2),而不会出现任何中间溢出或下溢。
Math.ceil() 它在十进制或小数值的情况下用于查找大于或等于传递给它的参数的最小整数值。
Math.floor() 它也用于十进制或小数值的情况,但要找到小于或等于传递给它的参数的最大整数值。
Math.getExponent() 它用于返回在表示值中使用的无偏指数。
Math.log() 它接受一个double值作为输入并返回该double值的自然对数。
Math.log10() 它需要一个double值作为输入,用于返回该double值的基数10对数。
Math.log1p() 它接受一个double值作为输入并返回该double值的自然对数。
Math.exp() 它将“e”返回到双倍值的幂,这也称为数字的指数,其中“e”是欧拉数,它大约等于2.71828。
Math.expm1() 它将'e'返回到双倍值的幂,这也称为从中减去一个数的指数。
Math.sin() 它需要一个double值作为参数,并返回Given double值的三角正弦值。
Math.cos() 它需要一个double值作为参数,并返回Given double值的三角余弦值。
Math.tan() 它需要一个double值作为参数,并返回给定double值的三角Tangent值。
Math.asin() 它用于返回给定值的三角Arc Sine值。
Math.acos() 它用于返回给定值的三角Arc Cosine值。
Math.sinh() 它需要一个double值作为参数,并返回Given double值的双曲正弦值。
Math.cosh() 它需要一个double值作为参数,并返回Given double值的双曲余弦值。
Math.tanh() 它需要一个double值作为其参数,并返回Given double值的双曲线Tangent值。

以下程序代码是一个在Java中实现Math类的一些方法的示例:

import java.lang.Math;  public class MathDemo {     public static void main(String() args) {          double a1= 4;         double a2= 100;         double a3= -58;         double a4= 60;          System.out.println("Square root of a1= "+ Math.sqrt(a1));         System.out.println("Square root of a2= "+ Math.sqrt(a2));          System.out.println("Cube root of a4 "+ Math.cbrt(a4));          System.out.println("Maximum of a1 and a4= "+ Math.max(a1,a4));         System.out.println("Minimum of a2 and a3= "+ Math.min(a2,a3));          System.out.println("The absolute value for a3= "+ Math.abs(a3)); 	 	System.out.println("The 3rd Power value for a1= "+ Math.pow(a3,3)); 	System.out.println("Log value of a1 is = "+ Math.log(a1)); 	System.out.println("Log10 value of a1 is= "+ Math.log10(a1)); 	System.out.println("Exp of a1 = "+ Math.exp(a1)); //return a power of 2 	 	// Now we will calculate trigonomatry values 	//First we have convert one value to radian 	// so here we will convert a4 to radian 	 	 double a = Math.toRadians(a4); 	System.out.println("Sine value of a is: " +Math.sin(a));                       System.out.println("Cosine value of a is: " +Math.cos(a));                      System.out.println("Tangent value of a is: " +Math.tan(a));   	  	// return the trigonometric arc sine of a               System.out.println("Sine value of a is: " +Math.asin(a));                        // return the trigonometric arc cosine value of a           System.out.println("Cosine value of a is: " +Math.acos(a));                      // return the trigonometric arc tangent value of a           System.out.println("Tangent value of a is: " +Math.atan(a));              // return the hyperbolic sine of a               System.out.println("Sine value of a is: " +Math.sinh(a));                        // return the hyperbolic cosine value of a           System.out.println("Cosine value of a is: " +Math.cosh(a));                      // return the hyperbolic tangent value of a           System.out.println("Tangent value of a is: " +Math.tanh(a));       } }

请参阅以下输出。

Java示例中的Math类

最后,Java示例中的Math类Java Math Class Tutorial结束了。

推荐帖子

Java字典类示例| Java教程中的字典类

嵌套类在Java示例中| Java嵌套类教程

Java类型转换示例|在Java教程中输入Casting

Java二进制搜索程序| Java示例中的二进制搜索

equalsIgnoreCase()在Java示例中| Java String equalsIgnoreCase()方法

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢