Home » Java programming language

Java StrictMath cbrt() method with example

StrictMath Class cbrt() method: Here, we are going to learn about the cbrt() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 25, 2019

StrictMath Class cbrt() method

  • cbrt() method is available in java.lang package.
  • cbrt() method is used to find the cube root of the given parameter in the method. Here, cbrt stands for cube root.
  • cbrt() method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • In this method, if we pass a positive parameter so it returns the cube root of the given parameter with the same sign(Positive) else if we pass negative parameter so it returns the cube root of the given parameter with the same sign(Negative).
  • cbrt() method does not throw any exception.

Syntax:

    public static double cbrt(double d);

Parameter(s):

  • double d – represents a double type value whose cube root to be found.

Return value:

The return type of this method is double – it returns the cube root of given angle.

Note:

  • If we pass NaN as an argument, method returns the same value (NaN).
  • If we pass zero (0), method returns the same value with the same sign.
  • If we pass an infinity, method returns the same value with the same sign.

Example:

// Java program to demonstrate the example
// of  cbrt(double d) method of StrictMath Class.

public class Cbrt {
    public static void main(String[] args) {
        // variable declarations
        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -7.0 / 0.0;
        double d4 = 7.0 / 0.0;
        double d5 = 1000.0;
        double d6 = -1000.0;

        // Display previous value of d1,d2,d3,d4,d5 and d6
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);
        System.out.println("d6: " + d6);

        // Here , we will get (-0.0) because we are 
        // passing parameter (-0.0) so the cube root is the same
        System.out.println("StrictMath.cbrt(d1): " + StrictMath.cbrt(d1));

        // Here , we will get (0.0) because we are 
        // passing parameter (0.0) so the cube root is the same
        System.out.println("StrictMath.cbrt(d2): " + StrictMath.cbrt(d2));

        // Here , we will get (-Infinity) because we are 
        // passing parameter (-7.0/0.0) so the cube root is (-Infinity)
        System.out.println("StrictMath.cbrt(d3): " + StrictMath.cbrt(d3));

        // Here , we will get (Infinity) because we are 
        // passing parameter (7.0/0.0) so the cube root is (Infinity)
        System.out.println("StrictMath.cbrt(d4): " + StrictMath.cbrt(d4));

        // Here , we will get (10.0) because we are
        // passing parameter (1000.0) so the cube root is 10.0
        System.out.println("StrictMath.cbrt(d5): " + StrictMath.cbrt(d5));

        // Here , we will get (-10.0) because we are
        // passing parameter (-1000.0) so the cube root is (-10.0)
        System.out.println("StrictMath.cbrt(d6): " + StrictMath.cbrt(d6));
    }
}

Output

d1: -0.0
d2: 0.0
d3: -Infinity
d4: Infinity
d5: 1000.0
d6: -1000.0
StrictMath.cbrt(d1): -0.0
StrictMath.cbrt(d2): 0.0
StrictMath.cbrt(d3): -Infinity
StrictMath.cbrt(d4): Infinity
StrictMath.cbrt(d5): 10.0
StrictMath.cbrt(d6): -10.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.