Appearance
Java Math class
Math class—a magical toolbox filled with powerful mathematical functions and constants. Whether you need to perform basic arithmetic, compute trigonometric functions, or generate random numbers, the Math class has got your back!
Some of features in the Math class has to offer:
Basic Arithmetic: Need to add, subtract, multiply, or divide numbers? The Math class has you covered with methods like
add
,subtract
,multiply
, anddivide
.Trigonometry: Want to calculate sine, cosine, or tangent values? Just summon the
sin
,cos
, andtan
methods from the Math class, and you're ready to solve any triangle mystery!Exponential and Logarithmic Functions: Need to raise a number to a power or find its logarithm? Look no further than the
pow
andlog
methods of the Math class.Constants: Want to use common mathematical constants like π (pi) or e (Euler's number)? The Math class provides constants like
PI
andE
for your convenience.Random Numbers: Feeling lucky? Use the
random
method to generate random numbers between 0 and 1, and let chance guide your adventures!
Now, let's sprinkle some magic into our Java code with the Math class:
java
public class MathMagic {
public static void main(String[] args) {
double radius = 5;
double area = Math.PI * Math.pow(radius, 2);
System.out.println("The area of the circle is: " + area);
double angle = 45;
double radians = Math.toRadians(angle);
double sinValue = Math.sin(radians);
System.out.println("The sine of 45 degrees is: " + sinValue);
int randomNumber = (int) (Math.random() * 100);
System.out.println("A random number between 0 and 100 is: " + randomNumber);
}
}
In this magical incantation, we calculate the area of a circle, find the sine of an angle, and generate a random number—all thanks to the mystical powers of the Math class!
Here's a list of the methods available in the Math
class in Java
Method | Description |
---|---|
abs(int a) | Returns the absolute value of an int value. |
abs(long a) | Returns the absolute value of a long value. |
abs(float a) | Returns the absolute value of a float value. |
abs(double a) | Returns the absolute value of a double value. |
acos(double a) | Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. |
asin(double a) | Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2. |
atan(double a) | Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. |
atan2(double y, double x) | Converts rectangular coordinates (x, y) to polar (r, theta) and returns the theta component. |
cbrt(double a) | Returns the cube root of a double value. |
ceil(double a) | Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. |
copySign(double magnitude, double sign) | Returns the first floating-point argument with the sign of the second floating-point argument. |
copySign(float magnitude, float sign) | Returns the first floating-point argument with the sign of the second floating-point argument. |
cos(double a) | Returns the trigonometric cosine of an angle. |
cosh(double x) | Returns the hyperbolic cosine of a double value. |
exp(double a) | Returns Euler's number e raised to the power of a double value. |
expm1(double x) | Returns ex -1. |
floor(double a) | Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. |
getExponent(float f) | Returns the unbiased exponent used in the representation of a float. |
getExponent(double d) | Returns the unbiased exponent used in the representation of a double. |
hypot(double x, double y) | Returns sqrt(x2 +y2) without intermediate overflow or underflow. |
IEEEremainder(double f1, double f2) | Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. |
log(double a) | Returns the natural logarithm (base e) of a double value. |
log10(double a) | Returns the base 10 logarithm of a double value. |
log1p(double x) | Returns the natural logarithm of the sum of the argument and 1. |
max(int a, int b) | Returns the greater of two int values. |
max(long a, long b) | Returns the greater of two long values. |
max(float a, float b) | Returns the greater of two float values. |
max(double a, double b) | Returns the greater of two double values. |
min(int a, int b) | Returns the smaller of two int values. |
min(long a, long b) | Returns the smaller of two long values. |
min(float a, float b) | Returns the smaller of two float values. |
min(double a, double b) | Returns the smaller of two double values. |
nextAfter(double start, double direction) | Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
nextAfter(float start, double direction) | Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
nextDown(double d) | Returns the floating-point value adjacent to d in the direction of negative infinity. |
nextDown(float f) | Returns the floating-point value adjacent to f in the direction of negative infinity. |
nextUp(double d) | Returns the floating-point value adjacent to d in the direction of positive infinity. |
nextUp(float f) | Returns the floating-point value adjacent to f in the direction of positive infinity. |
pow(double a, double b) | Returns the value of the first argument raised to the power of the second argument. |
random() | Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. |
round(float a) | Returns the closest int to the argument, with ties rounding to positive infinity. |
round(double a) | Returns the closest long to the argument, with ties rounding to positive infinity. |
scalb(float d, int scaleFactor) | Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. |
scalb(double d, int scaleFactor) | Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. |
signum(double d) | Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. |
sin(double a) | Returns the trigonometric sine of an angle. |
sinh(double x) | Returns the hyperbolic sine of a double value. |
sqrt(double a) | Returns the correctly rounded positive square root of a double value. |
tan(double a) | Returns the trigonometric tangent of an angle. |
tanh(double x) | Returns the hyperbolic tangent of a double value. |
toDegrees(double angrad) | Converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
toRadians(double angdeg) | Converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
ulp(float f) | Returns the size of an ulp of the argument. |
ulp(double d) | Returns the size of an ulp of the argument. |
These methods provide various mathematical functionalities in Java Math class.