Home » Java programming language

Java Math Class static double cbrt(double d) with example

Java Math Class static double cbrt(double d) method: Here, we are going to learn about the static double cbrt(double d) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on August 22, 2019

Math Class static double cbrt(double d)

  • This method is available in java.lang package.
  • This method is used to find the cube root of the given parameter in the method.
  • In this method, cbrt stands for cube root.
  • This is a static method so this method is accessible with the class name too.
  • The return type of this method is double that means it returns the cube root of the given parameter which is of the double datatype.
  • In this method, we pass only one parameter as an argument in the method of Math class and pass only the parameter for which we have to find the cube root.
  • In this method, if we pass 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).
  • This method does not throw any exception.

Syntax:

    public static double cbrt(double d){
    }

Parameter(s):

double d – A double value whose cube root to be found.

Note:

  • If we pass "NaN", it returns "NaN".
  • If we pass zero (-0 or 0), it returns the same value.
  • If we pass an infinity, it returns the same value i.e. an infinity.

Return value:

The return type of this method is double, it returns the cube root of the given value.

Java program to demonstrate example of cbrt(double d) method

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

class CbrtMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        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("Old value of d1 before implementation is :" + d1);
        System.out.println("Old value of d2 before implementation is :" + d2);
        System.out.println("Old value of d3 before implementation is :" + d3);
        System.out.println("Old value of d4 before implementation is :" + d4);
        System.out.println("Old value of d5 before implementation is :" + d5);
        System.out.println("Old value of d6 before implementation is :" + d6);

        // Here, we will get (-0.0) because we are passing parameter 
        // (-0.0) so the cube root is the same
        System.out.println("New value of d1 after implementation is :" + Math.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("New value of d2 after implementation is :" + Math.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("New value of d3 after implementation is :" + Math.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("New value of d4 after implementation is :" + Math.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("New value of d5 after implementation is :" + Math.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("New value of d6 after implementation is :" + Math.cbrt(d6));
    }
}

Output

E:\Programs>javac CbrtMethod.java

E:\Programs>java CbrtMethod
Old value of d1 before implementation is :-0.0
Old value of d2 before implementation is :0.0
Old value of d3 before implementation is :-Infinity
Old value of d4 before implementation is :Infinity
Old value of d5 before implementation is :1000.0
Old value of d6 before implementation is :-1000.0

New value of d1 after implementation is :-0.0
New value of d2 after implementation is :0.0
New value of d3 after implementation is :-Infinity
New value of d4 after implementation is :Infinity
New value of d5 after implementation is :10.0
New value of d6 after implementation is :-10.0


Comments and Discussions!

Load comments ↻





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