Java's Math Class
Java's Math Class provides a collection of methods for performing mathematical operations. Since it is built into the Java language, you don’t need to import it explicitly.
The Math class contains methods for basic arithmetic operations, trigonometry, exponentiation, and more. Since all the methods are static, you can call them directly using Math.methodName().
There are a couple of constants in the Math class.
Math.PI - Returns the mathematical constant π (3.14159...)
Math.E - Returns Euler’s number (2.71828...)
Rounding Methods
Math.ceil(double) - Rounds up to the next whole value.
Math.floor(double) - Rounds the value down.
Math.round(float or double) - Rounds up or down to the nearest whole value. (The return type matches your argument type)
Example:
public class MathClass {
public static void main(String args[]) {
double myDouble = Math.round(3.14);
System.out.println(myDouble); //output 3.0
} //main
} //MathClass
Math.abs(value) - Returns the absolute value. (Can be an int, float, double, or long)
Math.signum(float or double) - Returns the sign of a number ("-1.0" if less than 0, "0.0" if equal to 0, or "1.0" if greater than 0).
Example:
public class MathClass {
public static void main(String args[]) {
double myDouble = Math.signum(-3.14);
System.out.println(myDouble); //output -1.0
} //main
} //MathClass
Methods for getting the maximum or minimum value.
Math.max(a, b) - Returns the larger of `a` and `b`.
Math.min(a, b) - Returns the smaller of `a` and `b`.
What is interesting about these methods is that they take either two ints, two floats, two doubles, or two longs as the types. Well, we are just gonna have to see what happens when we mix types. Lets try this...
public class MathClass { public static void main(String args[]) { double myDouble = Math.max(-3.14, 1.2f); //float gets automatic type cast to double System.out.println(myDouble); } //main } //MathClass
This works but look at the result... 1.2000000476837158
It seems that when the float was type cast to a double, we picked up a little garbage. (Raises one eyebrow.) We will have to keep an eye on you Java.
Exponentiation and Logarithm methods.
Math.pow(a, b) - Returns 'a' raised to the power 'b'.
Math.sqrt(a) - Returns the square root of 'a'.
Math.cbrt(a) - Returns the cube root of 'a'.
Math.log(a) - Returns the natural logarithm (base 'e' also known as Euler's number also Math.E) of 'a'.
Math.log10(a) - Returns the base10 logarithm of 'a'.
Math.exp(a) - Returns base 'e' raised to the power 'a'.
Example:
public class MathClass { public static void main(String args[]) { System.out.println(Math.pow(2, 3)); // Output: 8.0 System.out.println(Math.sqrt(16)); // Output: 4.0 System.out.println(Math.cbrt(27)); // Output: 3.0 System.out.println(Math.log(Math.E)); // Output: 1.0 System.out.println(Math.log10(100)); // Output: 2.0 System.out.println(Math.exp(1)); // Output 2.718281828459045 (Math.E) } //main } //MathClass
Trigonometric Functions
Math.sin(a), Math.cos(a), and Math.tan(a) - Returns sine, cosine, and tangent of angle 'a' in radians. Check out my tutorial on radians. Working with Radians
Math.asin(a), Math.acos(a), and Math.atan(a) - Returns the inverse trigonometric values.
Math.toRadians(a), Math.toDegrees(a) - Converts between degrees and radians.
Example:
public class MathClass { public static void main(String args[]) { double myDouble = Math.toRadians(90); // convert 90 degrees to radians System.out.println(Math.sin(myDouble)); // Output: 1.0 } //main } //MathClass
The Math Class has a method for generationg random numbers
Math.random() - Returns a random double value between '0.0' and less than '1.0'. (The output would never be '1.0' because '1.0' is excluded.)
Another interesting method is Math.addExact(a, b). It returns the sum of two values, throwing an exception if the result overflows. This is useful if you are adding really big numbers.
Lets play with this one...
public class MathClass {
public static void main(String args[]) {
int a = Integer.MAX_VALUE; // 2147483647
int b = 1;
System.out.println(a + b); // Output: -2147483648
//An overflow occured! Yikes no warning!
} //main
} //MathClass
public class MathClass {
public static void main(String args[]) {
int a = Integer.MAX_VALUE; // 2147483647
int b = 1;
System.out.println(Math.addExact(a, b)); // Output: Exception thrown
} //main
} //MathClass
public class MathClass {
public static void main(String args[]) {
int a = Integer.MAX_VALUE; // 2147483647
int b = 1;
try {
int result = Math.addExact(a, b); // The overflow happens here
System.out.println("Sum: " + result);
} catch (ArithmeticException e) {
System.out.println("Overflow occurred: " + e.getMessage());
}
} //main
} //MathClass
That was fun. And there are more methods in Java's Math Class so check them out at Java Class Math
Thanks for reading.
Comments
Post a Comment