The Math.cbrt() is a part of java.lang.Math package. This method is used to calculate the cube root of a given number. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.
Note: A number, when multiplied by itself three times, gives the original number, is known as a cube root of that number. For example, the cube root of 27 is 3 because 3 × 3 × 3 = 27.
Special Cases:
- If the number is positive, this method will give a positive cube root.
- If the number is negative, this method will give a negative cube root.
- If the number is zero, this method will give zero.
- If the number is infinity, this method will give infinity with the same sign.
- If the number is NaN, this method will give NaN.
These special cases make sure that the Math.cbrt() methods work correctly.
Syntax of cbrt() Method
public static double cbrt(double a)
- Parameter: This method takes a single parameter a, of type double. This is the number whose cube root will be calculated.
- Return Type: This method returns the cube root of the given number a.
Now, we are going to discuss some examples for better understanding.
Examples of Java Math.cbrt() Method
Example 1: In this example, we will see the basic usage of cbrt() method with regular values.
Java
// Java Program to demonstrate
// the working of cbrt() method
import java.lang.Math;
class Geeks {
public static void main(String args[]) {
// Defining different test values
double a = 125.0;
// Positive Infinity
double b = 1.0 / 0;
// Negative Infinity
double c = -(1.0 / 0);
// Positive Zero
double d = 0.0;
// Negative Zero
double e = -0.0;
System.out.println(Math.cbrt(a));
System.out.println(Math.cbrt(b));
System.out.println(Math.cbrt(c));
System.out.println(Math.cbrt(d));
System.out.println(Math.cbrt(e));
}
}
Output5.0
Infinity
-Infinity
0.0
-0.0
Explanation: Here, we are calculating the cube roots of different numbers like the cube root of 125.0 is 5.0 and for positive infinity, the cube root is positive infinity and for negative infinity, the cube root is negative infinity and for both the positive and negative zero the cube root will always be zero.
Example 2: In this example, we will see howcbrt() method handles NaN and Infinity.
Java
// Handling NaN and Infinity
import java.lang.Math;
class Geeks {
public static void main(String args[]) {
// Defining test values for Infinity and NaN
double p = 1.0 / 0;
double n = -(1.0 / 0);
double nan = 0.0 / 0.0;
System.out.println("Cube root of Positive Infinity: "
+ Math.cbrt(p));
System.out.println("Cube root of Negative Infinity: "
+ Math.cbrt(n));
System.out.println("Cube root of NaN: "
+ Math.cbrt(nan));
}
}
OutputCube root of Positive Infinity: Infinity
Cube root of Negative Infinity: -Infinity
Cube root of NaN: NaN
Explanation: Here, we are calculating the cube roots of special values like positive infinity, negative infinity and NaN. The cube root of postitive infinity is positive infinity, the cube root of negative infinity is negative infinity and the cube root for NaN is NaN.
Important Points:
- This method is used when we need to calculate the cube root of any number.
- This method can work with any type of number like positive, negative and zero, can also work well with infinity and NaN.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read