Java 中的 sqrt() 方法

Mehvish Ashiq 2023年10月12日
  1. 在 Java 中使用 sqrt() 方法求数的平方根
  2. 在 Java 中不使用 sqrt() 方法求数字的平方根
Java 中的 sqrt() 方法

本教程通过各种代码示例演示 Java 中的 sqrt() 方法。它还介绍了 Math.pow() 方法和一个自定义公式,我们可以使用该公式来查找数字的平方根,而无需使用 sqrt() 方法。

在 Java 中使用 sqrt() 方法求数的平方根

java.lang.Math 包包含 sqrt() 方法。它返回类型为 double 的数字的平方根,并作为参数传递给 sqrt() 方法。

如果传递的参数是 NaN 或负数,则返回 NaN

如果我们将正无穷大作为参数传递,sqrt() 函数会输出正无穷大。而且,如果 sqrt() 方法得到负数或正数零,结果将与参数相同。

让我们使用各种示例代码来探索所有场景。

示例代码(如果传递的参数是 double 类型的正数):

import java.lang.Math;

public class calSquareRoot {
  public static void main(String args[]) {
    double number = 25;
    double sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);
  }
}

输出:

The square root of 25.0 is 5.0

此代码示例表明我们必须导入 java.lang.Math 包才能在 Java 程序中使用 sqrt() 方法。在 main 方法中,我们声明并初始化一个 double 类型的变量 number,其值为 25

然后,我们将此 number 传递给 sqrt() 方法并将返回值存储在 sqrt 变量中,该变量在 System.out.println 中进一步用于打印指定 number 的正方形根。

对于本节中所有即将出现的示例,该过程将保持不变,但我们将更新 number 的值以尝试不同的情况。

示例代码(如果传递的参数是 NaN):

import java.lang.Math;

public class calSquareRoot {
  public static void main(String args[]) {
    double number = Double.NaN;
    double sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);
  }
}

输出:

The square root of NaN is NaN

示例代码(如果传递的参数是负数):

import java.lang.Math;

public class calSquareRoot {
  public static void main(String args[]) {
    double number = -5;
    double sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);
  }
}

输出:

The square root of -5.0 is NaN

示例代码(如果传递的参数是正无穷大):

import java.lang.Math;

public class calSquareRoot {
  public static void main(String args[]) {
    double number = Double.POSITIVE_INFINITY;
    double sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);
  }
}

输出:

The square root of Infinity is Infinity

示例代码(如果传递的参数是负零或正零):

import java.lang.Math;

public class calSquareRoot {
  public static void main(String args[]) {
    double number = -0;
    double sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);

    number = 0;
    sqrt = Math.sqrt(number);
    System.out.println("The square root of " + number + " is " + sqrt);
  }
}

输出:

The square root of 0.0 is 0.0
The square root of 0.0 is 0.0

我们已经涵盖了本节开头讨论的所有可能的示例。是时候探索其他不使用 sqrt() 方法来求平方根的方法了。

在 Java 中不使用 sqrt() 方法求数字的平方根

在本节中,我们将介绍 Math.pow() 方法和如下自定义公式。

$$ \sqrt{n+1} = \frac {(\sqrt{n} + \frac {number} {\sqrt {n}})} {2.0} $$

我们将确保这两种方法都满足下面给出的 sqrt() 方法的规则。

  1. 返回正数的平方根。
  2. 如果传递了 NaN 或负数,则返回 NaN
  3. 如果负零或正零返回相同的传递数。
  4. 如果得到 Double.POSITIVE_INFINITY,则返回 Infinity

在 Java 中使用 Math.pow() 方法求数的平方根

示例代码:

public class calSquareRoot {
  public static void main(String[] args) {
    double positiveNumber = 25;
    double negativeNumber = -25;
    double nan = Double.NaN;
    double postiveInfinity = Double.POSITIVE_INFINITY;
    double positiveZero = 0;
    double negativeZero = -0;
    double sqrt = 0;

    sqrt = Math.pow(positiveNumber, 0.5);
    System.out.println("The Square root of  " + positiveNumber + "  =  " + sqrt);

    sqrt = Math.pow(nan, 0.5);
    System.out.println("The Square root of  " + nan + "  =  " + sqrt);

    sqrt = Math.pow(negativeNumber, 0.5);
    System.out.println("The Square root of  " + negativeNumber + "  =  " + sqrt);

    sqrt = Math.pow(postiveInfinity, 0.5);
    System.out.println("The Square root of  " + postiveInfinity + "  =  " + sqrt);

    sqrt = Math.pow(positiveZero, 0.5);
    System.out.println("The Square root of  " + positiveZero + "  =  " + sqrt);

    sqrt = Math.pow(negativeZero, 0.5);
    System.out.println("The Square root of  " + negativeZero + "  =  " + sqrt);
  }
}

输出:

The Square root of  25.0  =  5.0
The Square root of  NaN  =  NaN
The Square root of  -25.0  =  NaN
The Square root of  Infinity  =  Infinity
The Square root of  0.0  =  0.0
The Square root of  0.0  =  0.0

正如我们所知,√number = number½在数学上是被认可的。所以,我们可以使用幂函数(Math.pow())来计算幂,也就是这里第一个参数的值提高到 0.5。

它是给定数字的平方根。

在 Java 中使用自定义公式方法查找数字的平方根

示例代码:

public class calSquareRoot {
  public static double squareRoot(double number) {
    if (number < 0 || Double.isNaN(number))
      return Double.NaN;

    else if (number == Double.POSITIVE_INFINITY)
      return Double.POSITIVE_INFINITY;

    else if (number == 0 || number == -0)
      return number;

    else if (number > 0) {
      double temp;
      double sqrt = number / 2;

      do {
        temp = sqrt;
        sqrt = (temp + (number / temp)) / 2;
      } while ((temp - sqrt) != 0);

      return sqrt;
    }

    else {
      return -1;
    }
  }

  public static void main(String[] args) {
    double positiveNumber = 25;
    double negativeNumber = -25;
    double nan = Double.NaN;
    double postiveInfinity = Double.POSITIVE_INFINITY;
    double positiveZero = 0;
    double negativeZero = -0;
    double sqrt = 0;

    sqrt = squareRoot(positiveNumber);
    System.out.println("The Square root of  " + positiveNumber + "  =  " + sqrt);

    sqrt = squareRoot(nan);
    System.out.println("The Square root of  " + nan + "  =  " + sqrt);

    sqrt = squareRoot(negativeNumber);
    System.out.println("The Square root of  " + negativeNumber + "  =  " + sqrt);

    sqrt = squareRoot(postiveInfinity);
    System.out.println("The Square root of  " + postiveInfinity + "  =  " + sqrt);

    sqrt = squareRoot(positiveZero);
    System.out.println("The Square root of  " + positiveZero + "  =  " + sqrt);

    sqrt = squareRoot(negativeZero);
    System.out.println("The Square root of  " + negativeZero + "  =  " + sqrt);
  }
}

输出:

The Square root of  25.0  =  5.0
The Square root of  NaN  =  NaN
The Square root of  -25.0  =  NaN
The Square root of  Infinity  =  Infinity
The Square root of  0.0  =  0.0
The Square root of  0.0  =  0.0

在这里,我们使用下面公式。

$$
\sqrt{n+1} = \frac {(\sqrt{n} + \frac {number} {\sqrt {n}})} {2.0}
$$

main 函数中,我们将不同的值传递给 squareRoot() 方法,其中我们有 if-else 条件来检查规则。

我们根据给定的求平方根的规则返回值,如果给定的数字大于 0,则执行给定的公式。请记住,第一个平方根数必须是 inputNumber/2

作者: Mehvish Ashiq
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

相关文章 - Java Method