Java 数学 – ceil() Floor() 方法

Java 已经有几个高级用途的应用程序,包括处理物理中的复杂计算、建筑/结构设计、处理地图和相应的纬度/经度等。

所有这些应用都需要使用复杂的计算/方程式,手动执行起来非常繁琐。从编程上讲,此类计算将涉及对数、三角函数、指数方程等的使用。

Java 数学

现在,您无法将所有对数或三角函数表硬编码到应用程序或数据的某个地方。数据会非常庞大​​,维护起来也很复杂。

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

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

让我们看一下 Java 数学课。

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

因此,Math 类 java 将这两个常量作为双精度字段提供。

数学E – 具有如下值 2.718281828459045

Math.PI – 具有如下值 3.141592653589793

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

付款方式 描述 参数
ABS 返回参数的绝对值 Double, 浮点数, 整数, 长整型
返回封闭的 int 或 long (根据参数) 双精度或浮点
细胞 数学 ceil 函数 Java 返回大于或等于参数的最小整数 Double
地板 Java floor 方法返回小于或等于参数的最大整数 Double
分钟 返回两个参数中最小的一个 Double, 浮点数, 整数, 长整型
最大 返回两个参数中最大的一个 Double, 浮点数, 整数, 长整型

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

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

整型变量

int i1 = 27;
int i2 = -45;

Double(十进制)变量

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() 方法示例

Math.round() 方法 Java 根据参数返回封闭的 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 方法示例

Math.ceil 和 Math.floor Java 方法用于返回大于或等于参数的最小和最大整数。下面是数学的下限和上限 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));

 }
}

我们将得到以下 math.ceil 的输出 Java 例。

预期产量:

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
历史记录 返回参数的自然对数 翻番
战俘 接受 2 个参数作为输入并返回第一个参数的第二个参数的幂的值 Double
地板 Java math floor 返回小于或等于参数的最大整数 Double
方格 返回参数的平方根 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)让我们看看下面的表格,它向我们展示了 三角函数方法 及其描述-

付款方式 描述 参数
返回指定参数的正弦值 Double
购物车 返回指定参数的余弦值 翻番
黄褐色 返回指定参数的正切 Double
阿坦2 将直角坐标 (x, y) 转换为极坐标 (r, theta) 并返回 theta Double
到度 将参数转换为度数 Double
方格 返回参数的平方根 Double
转弧度 将参数转换为弧度 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 设计自己的科学计算器。