Java Math – ceil() Floor() 方法

Java 在几个高级应用领域都有应用,包括处理物理学中的复杂计算、结构的设计/工程、地图和相应的纬度/经度处理等。

所有这些应用都需要使用复杂的计算/方程,这些计算/方程手动执行起来非常繁琐。在程序中,这类计算将涉及对数、三角学、指数方程等的用法。

Java Math

现在,您不能将所有对数或三角学表格硬编码在应用程序或数据中的某个地方。数据量会非常庞大,并且难以维护。

Java 为此目的提供了一个非常有用的类。这就是 Math Java 类 (java.lang.Math)。

该类提供了执行指数、对数、根号和三角方程等运算的方法。

让我们看一下 Java Math 类提供的方法。

Math 中的两个最基本元素是“e”(自然对数的底数)和“pi”(圆的周长与其直径的比率)。这两个常数在上述计算/运算中经常需要。

因此,Math 类 Java 以双精度字段形式提供了这两个常数。

Math.E – 值为 2.718281828459045

Math.PI – 值为 3.141592653589793

A) 让我们看一下下表,它展示了基本方法及其描述

方法 描述 参数
abs 返回参数的绝对值 Double, float, int, long
round 返回最接近的 int 或 long(取决于参数) double 或 float
ceil Java 中的 Math ceil 函数返回大于或等于参数的最小整数 Double
floor Java floor 方法返回小于或等于参数的最大整数 Double
min 返回两个参数中较小的一个 Double, float, int, long
max 返回两个参数中较大的一个 Double, float, int, long

下面是上述方法的代码实现

注意:无需显式导入 java.lang.Math,因为它会自动导入。其所有方法都是静态的。

整数变量

int i1 = 27;
int i2 = -45;

Double(小数)变量

double d1 = 84.6;
double d2 = 0.45;

Java Math 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) 的参数次方 Double
日志 返回参数的自然对数 double
Pow 接受两个参数作为输入,并返回第一个参数的第二个参数次方的值 Double
floor Java math floor 返回小于或等于参数的最大整数 Double
Sqrt 返回参数的平方根 Double

下面是上述方法的代码实现:(使用了与上面相同的变量)

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) 让我们看一下下表,它展示了三角学方法及其描述-

方法 描述 参数
Sin 返回指定参数的正弦值 Double
Cos 返回指定参数的余弦值 double
Tan 返回指定参数的正切值 Double
Atan2 将直角坐标 (x, y) 转换为极坐标 (r, theta) 并返回 theta Double
toDegrees 将参数转换为度数 Double
Sqrt 返回参数的平方根 Double
toRadians 将参数转换为弧度 Double

默认参数为弧度

下面是代码实现

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 设计自己的科学计算器。