Java Math Abs() Round() Ceil() Floor() Min() 方法/函数与示例
Java 有几个高级应用程序,包括处理物理中的复杂计算、架构/结构设计、处理地图和相应的纬度/经度等。
在本 Java 教程中,您将学习:
- Java 中的 Math.abs
- Java 中的 Math.round
- Java 中的 Math.ceil 和 Math.floor
- Java 中的 Math.min
所有此类应用程序都需要使用复杂的计算/方程式,手动执行这些计算/方程式很乏味。以编程方式,此类计算将涉及对数、三角函数、指数方程等的使用。
现在,您不能在应用程序或数据的某处硬编码所有日志或三角表。这些数据将是庞大且复杂的维护。
Java为此提供了一个非常有用的类。它是 Math java 类(java.lang.Math)。
该类还提供了执行指数、对数、根和三角方程等运算的方法。
让我们看一下Java Math类提供的方法。
数学中两个最基本的元素是“e”(自然对数的底)和“pi”(圆的周长与其直径的比率)。上述计算/运算中经常需要这两个常数。
因此,Java 的 Math 类将这两个常量作为双字段提供。
数学.E – 值为 2.718281828459045
数学.PI - 值为 3.141592653589793
A) 让我们看看下表,它向我们展示了基本方法 及其说明
方法 | 说明 | 参数 |
---|---|---|
腹肌 | 返回参数的绝对值 | 双精度型、浮点型、整数型、长型 |
圆形 | 返回封闭的 int 或 long(根据参数) | 双精度或浮点数 |
细胞 | Java 中的数学 ceil 函数返回大于或等于参数的最小整数 | 双 |
地板 | Java floor方法返回小于等于参数的最大整数 | 双 |
分钟 | 返回两个参数中最小的一个 | 双精度型、浮点型、整数型、长型 |
最大值 | 返回两个参数中最大的一个 | 双精度型、浮点型、整数型、长型 |
下面是上述方法的代码实现:
注意:无需显式导入 java.lang.Math 作为其隐式导入。它的所有方法都是静态的。
整型变量
int i1 = 27; int i2 = -45;
双(十进制)变量
double d1 = 84.6; double d2 = 0.45;
Java 数学 abs() 方法与示例
Java Math abs() 方法返回参数的绝对值。
public class Guru99 { public static void main(String args[]) { int i1 = 27; int i2 = -45; double d1 = 84.6; double d2 = 0.45; System.out.println("Absolute value of i1: " + Math.abs(i1)); System.out.println("Absolute value of i2: " + Math.abs(i2)); System.out.println("Absolute value of d1: " + Math.abs(d1)); System.out.println("Absolute value of d2: " + Math.abs(d2)); } }
输出:
Absolute value of i1: 27 Absolute value of i2: 45 Absolute value of d1: 84.6 Absolute value of d2: 0.45
Java Math.round() 方法与示例
Java 中的 Math.round() 方法根据参数返回封闭的 int 或 long。下面是 math.round Java 方法的例子。
public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("Round off for d1: " + Math.round(d1)); System.out.println("Round off for d2: " + Math.round(d2)); } }
输出:
Round off for d1: 85 Round off for d2: 0
Java Math.ceil 和 Math.floor 方法与示例
Java 方法中的 Math.ceil 和 Math.floor 用于返回大于或等于参数的最小和最大整数。下面是 Math floor 和 ceiling Java 示例。
public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("Ceiling of '" + d1 + "' = " + Math.ceil(d1)); System.out.println("Floor of '" + d1 + "' = " + Math.floor(d1)); System.out.println("Ceiling of '" + d2 + "' = " + Math.ceil(d2)); System.out.println("Floor of '" + d2 + "' = " + Math.floor(d2)); } }
我们将在 Java 示例中获得 math.ceil 的以下输出。
输出:
Ceiling of '84.6' = 85.0 Floor of '84.6' = 84.0 Ceiling of '0.45' = 1.0 Floor of '0.45' = 0.0
带有示例的 Java Math.min() 方法
Java Math.min() 方法返回两个参数中的最小值。
public class Guru99 { public static void main(String args[]) { int i1 = 27; int i2 = -45; double d1 = 84.6; double d2 = 0.45; System.out.println("Minimum out of '" + i1 + "' and '" + i2 + "' = " + Math.min(i1, i2)); System.out.println("Maximum out of '" + i1 + "' and '" + i2 + "' = " + Math.max(i1, i2)); System.out.println("Minimum out of '" + d1 + "' and '" + d2 + "' = " + Math.min(d1, d2)); System.out.println("Maximum out of '" + d1 + "' and '" + d2 + "' = " + Math.max(d1, d2)); } }
输出:
Minimum out of '27' and '-45' = -45 Maximum out of '27' and '-45' = 27 Minimum out of '84.6' and '0.45' = 0.45 Maximum out of '84.6' and '0.45' = 84.6
B) 让我们看看下表,它向我们展示了指数和对数方法 及其描述-
方法 | 说明 | 参数 |
---|---|---|
exp | 将自然对数 (e) 的基数返回到参数的幂 | 双 |
日志 | 返回参数的自然对数 | 双 |
Pow | 将 2 个参数作为输入,并返回第一个参数的第二个参数次方的值 | 双 |
地板 | Java math floor 返回小于或等于参数的最大整数 | 双 |
平方 | 返回参数的平方根 | 双 |
下面是上述方法的代码实现:(变量同上)
public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("exp(" + d2 + ") = " + Math.exp(d2)); System.out.println("log(" + d2 + ") = " + Math.log(d2)); System.out.println("pow(5, 3) = " + Math.pow(5.0, 3.0)); System.out.println("sqrt(16) = " + Math.sqrt(16)); } }
输出:
exp(0.45) = 1.568312185490169 log(0.45) = -0.7985076962177716 pow(5, 3) = 125.0 sqrt(16) = 4.0
C) 让我们看看下表,它向我们展示了 三角法 及其描述-
方法 | 说明 | 参数 |
---|---|---|
罪 | 返回指定参数的正弦值 | 双 |
cos | 返回指定参数的余弦 | 双 |
谭 | 返回指定参数的正切 | 双 |
Atan2 | 将直角坐标(x,y)转换为极坐标(r,theta)并返回theta | 双 |
toDegrees | 将参数转换为度数 | 双 |
平方 | 返回参数的平方根 | 双 |
到弧度 | 将参数转换为弧度 | 双 |
默认参数以弧度为单位
下面是代码实现:
public class Guru99 { public static void main(String args[]) { double angle_30 = 30.0; double radian_30 = Math.toRadians(angle_30); System.out.println("sin(30) = " + Math.sin(radian_30)); System.out.println("cos(30) = " + Math.cos(radian_30)); System.out.println("tan(30) = " + Math.tan(radian_30)); System.out.println("Theta = " + Math.atan2(4, 2)); } }
输出:
sin(30) = 0.49999999999999994 cos(30) = 0.8660254037844387 tan(30) = 0.5773502691896257 Theta = 1.1071487177940904
现在,有了以上内容,你也可以用java设计自己的科学计算器了。
java