Java math overviewJava’s built-in operators are useful, but they don’t come anywhere near providing all the mathematical needs of most Java programmers. That’s where the Math class comes in. It includes a bevy of built-in methods that perform a wide variety of mathematical calculations, from basic functions such as calculating an absolute value or a square root to trigonometry functions such as sin and cos, to practical functions such as rounding numbers or generating random numbers.
Constants of the Java Math classThe Math class defines two constants that are useful for many mathematical calculations. Below lists these constants. - PI 3.141592653589793
- E 2.71828182845904
Java math constants Examplei mport java.util.Scanner; public class CircleArea { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println( “Welcome to the circle area calculator.”); System.out.print(“Enter the radius of your circle: “); double r = sc.nextDouble(); double area = Math.PI * (r * r); System.out.println(“The area is “ + area); } } Java Mathematical FunctionsThe Math class defines a number of methods that provide trigonometric, logarithmic, exponential, and rounding operations, among others. This class is primarily useful with floating-point values. For the trigonometric functions, angles are expressed in radians. The logarithm and exponentiation functions are base e, not base 10. Here are some examples: double d = Math.toRadians(27); // Convert 27 degrees to radians d = Math.cos(d); // Take the cosine d = Math.sqrt(d); // Take the square root d = Math.log(d); // Take the natural logarithm d = Math.exp(d); // Do the inverse: e to the power d d = Math.pow(10, d); // Raise 10 to this power d = Math.atan(d); // Compute the arc tangent d = Math.toDegrees(d); // Convert back to degrees double up = Math.ceil(d); // Round to ceiling double down = Math.floor(d); // Round to floor long nearest = Math.round(d); // Round to nearest In Java 5.0, several new functions have been added to the Math class, including the following: double d = 27; d = Math.cbrt(d); // cube root d = Math.log10(d); // base-10 logarithm d = Math.sinh(d); // hyperbolic sine. Also cosh() and tanh() d = Math.hypot(3, 4); // Hypotenuse Below is some Java Mathematical Functions example:You can use the abs and signnum methods to force the sign of one variable to match the sign of another, like this: int a = 27; int b = -32; a = Math.abs(a) * Math.signum(b); // a is now -27; You can use the pow method to square a number, like this: double x = 4.0; double y = Math.pow(x, 2); // a is now 16; However, simply multiplying the number by itself is often just as easy and just as readable: double x = 4.0; double y = x * x; // a is now 16; Java Rounding functions(java math tutorial)The Math class has four methods that round or truncate float or double values. below lists these methods. As you can see, each of these methods uses a different technique to calculate an integer value that’s near the double or float value passed as an argument. Note that even though all four of these methods rounds a floating-point value to an integer value, only the round method actually returns an integer type (int or long, depending on whether the argument is a float or a double). The other methods return doubles that happen to be integer values. - ceil(argument) Returns the smallest double value that is an integer and is greater than or equal to the value of the argument.
- floor(argument) Returns the largest double value that is an integer and is less than or equal to the value of the argument.
- rint(argument) Returns the double value that is an integer and is closest to the value of the argument. If two integer values are equally
close, returns the one that is even. If the argument is already an integer, returns the argument value. - round(argument) Returns the integer that is closest to the argument. If the argument is a double, returns a long. If the argument is a
float, returns an int.
Java Rounding functions Example: public class RoundingDemo { public static void main(String[] args) { double x = 29.4; double y = 93.5; double z = -19.3; System.out.println(“round(x) = “ + Math.round(x)); System.out.println(“round(y) = “ + Math.round(y)); System.out.println(“round(z) = “ + Math.round(z)); System.out.println(); System.out.println(“ceil(x) = “ + Math.ceil(x)); System.out.println(“ceil(y) = “ + Math.ceil(y)); System.out.println(“ceil(z) = “ + Math.ceil(z)); System.out.println(); System.out.println(“floor(x) = “ + Math.floor(x)); System.out.println(“floor(y) = “ + Math.floor(y)); System.out.println(“floor(z) = “ + Math.floor(z)); System.out.println(); System.out.println(“rint(x) = “ + Math.rint(x)); System.out.println(“rint(y) = “ + Math.rint(y)); System.out.println(“rint(z) = “ + Math.rint(z)); } } This program shows how to use each of the four methods to round three different double values: 29.4, 93.5, and –19.3. Here’s the output from this program: round(x) = 29 round(y) = 94 round(z) = -19 ceil(x) = 30.0 ceil(y) = 94.0 ceil(z) = -19.0 floor(x) = 29.0 floor(y) = 93.0 floor(z) = -20.0 rint(x) = 29.0 rint(y) = 94.0 rint(z) = -19.0 Note that each of the four methods produces a different result for at least one of the values: - All the methods except ceil return 29.0 (or 29) for the value 29.4. ceil returns 30.0, which is the smallest integer that’s greater than 29.4.
- All the methods except floor return 94.0 (or 94) for the value 93.5.floor returns 93.0 because that’s the largest integer that’s less than 93.99. rint returns 94.0 because it’s an even number, and 93.5 is midway between 93.0 and 94.0.
- All the methods except floor return –19.0 (or –19) for –19.3. floor returns 2–20 because –20 is the largest integer that’s less than –19.3.
Java math Big NumbersThe java.math package contains the BigInteger and BigDecimal classes. These classes allow you to work with arbitrary-size and arbitrary-precision integers and floating-point values.
Java math Big Numbers example: import java.math.*; // Compute the factorial of 1000 BigInteger total = BigInteger.valueOf(1); for(int i = 2; i <= 1000; i++) total = total.multiply(BigInteger.valueOf(i)); System.out.println(total.toString()); |