Math Functions




The Math library (class) holds a lot of common math functions (methods). The methods are all static, so you can use Math. in order to call (invoke) the function (method). You can avoid putting Math. in front of all library references by placing a static import at the top of your program. import static java.lang.Math.*;

Examples:

Math functions:

Math.abs()       returns absolute value as either an int or a double
Math.min()       returns min as either an int or a double
Math.max()       returns max as either an int or a double
Math.sqrt()      returns the square root as a double
Math.round()     returns an int    rounds to nearest int
Math.floor()     returns a double  rounds down
Math.ceil()      returns a double  rounds up
Math.pow()       returns a double  raises to the power
Math.random()    returns a double (0.0 to 0.9999...
Math.sin()       returns the sin of the angle (radian argument)
Math.cos()       returns the cos of the angle (radian argument)
// remember radians = degrees * Math.PI/180
// and      degrees = radians * 180/Math.PI

Math constants
Math.PI          a constant that represents pi 3.14159...
Math.E           a constant that represents e 2.718282...

// using the Math class
int a = 7;
int b = -7;
int c = 49;
       	
double d = 23.2;
double e = 25;
double x = 12.257;
double y = 13.5;
double z = 13.7;


out.println(a);  // prints out 7
out.println(b);  // prints out -7
out.println(c);  // prints out 49
out.println(64); // prints out 64
out.println();
       		
       		
out.println(Math.abs(a));   // prints out 7
out.println(Math.abs(b));   // prints out 7
out.println(Math.abs(c));   // prints out 49
out.println(Math.abs(-5));  // prints out 5
out.println();
       	
out.println(Math.max(a,b)); // prints out 7
out.println(Math.min(a,b)); // prints out -7
out.println(Math.min(7,5)); // prints out 5
out.println();
       	
out.println(Math.sqrt(a));  // prints out 2.645...
out.println(Math.sqrt(b));  // runtime error
out.println(Math.sqrt(c));  // prints out 7.0
out.println(Math.sqrt(64)); // prints out 8.0
out.println();
       	
out.println(x); // prints out 12.257
out.println(y); // prints out 13.5
out.println(z); // prints out 13.7
out.println();

out.println(Math.round(x));   // prints out 12
out.println(Math.round(y));   // prints out 14
out.println(Math.round(z));   // prints out 14
out.println(Math.round(3.2)); // prints out 3
out.println(Math.round(3.5)); // prints out 4
out.println(Math.round(3.7)); // prints out 4
out.println();

out.println(Math.floor(x));   // prints out 12.0
out.println(Math.floor(y));   // prints out 13.0
out.println(Math.floor(z));   // prints out 13.0
out.println(Math.floor(3.2)); // prints out 3.0
out.println(Math.floor(3.5)); // prints out 3.0
out.println(Math.floor(3.7)); // prints out 3.0
out.println();

out.println(Math.ceil(x));    // prints out 13.0 
out.println(Math.ceil(y));    // prints out 14.0
out.println(Math.ceil(z));    // prints out 14.0
out.println(Math.ceil(3.2));  // prints out 4.0
out.println(Math.ceil(3.5));  // prints out 4.0
out.println(Math.ceil(3.7));  // prints out 4.0
out.println();
      	
out.println(Math.max(x,y));   // prints out 13.5
out.println(Math.min(x,y));   // prints out 12.257
out.println(Math.min(a,x));   // prints out 7.0 
out.println(Math.max(7,5));   // prints out 7
out.println(Math.min(7,5));   // prints out 5
out.println(Math.min(5,7));   // prints out 5
out.println();
       	
out.println(Math.pow(a,2));   // prints out 49.0
out.println(Math.pow(2,3));   // prints out 8.0
out.println(Math.pow(2,5));   // prints out 32.0
out.println();

out.println(Math.PI);   // prints out 3.14159...
out.println();
		 	
out.println(Math.random());            // prints out 0.0 to 0.9...
out.println((int)(Math.random()*6));   // prints out 0 to 5
out.println((int)(Math.random()*6+1)); // prints out 1 to 6
out.println((int)(Math.random()*10));  // prints out 0 to 9
out.println((int)(Math.random()*8));   // prints out 0 to 7 
out.println((int)(Math.random()*20));  // prints out 0 to 19 
out.println((int)(Math.random()*10+5));  // prints out 5 to 14 
out.println((int)(Math.random()*100+10));  // prints out 10 to 109 
out.println();
		


Example: // this is a comment // comments are ignored by the compiler (translater) import java.util.Scanner; public class Sample6 { public Sample6() { } public static void main(String[] args) { // we will create a Scanner object to read from the keyboard Scanner scan = new Scanner (System.in); // this is the title System.out.println("Miles Per Hour"); System.out.println(); System.out.println(); // we will now ask the user to enter the distance travelled System.out.print("Enter the distance travelled "); double d = scan.nextDouble(); System.out.println(); // we will now ask the user to enter the hours System.out.print("Enter the hours "); double h = scan.nextDouble(); System.out.println(); // we will now ask the user to enter the minutes System.out.print("Enter the minutes "); double m = scan.nextDouble(); System.out.println(); System.out.println(); // we will now find the mph // note that Math.round returns a double even though // it rounds to the nearest whole number int mph = (int)Math.round(d/(h+m/60)); System.out.println("You traveled " + d + " miles in " + h + " hours and " + m + " minutes"); System.out.println(); System.out.println("You were going " + mph + " mph"); System.out.println(); System.out.println(); } }
Example: import java.util.Scanner; public class Sample5 { public Sample5() { } public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("Area of a Circle"); System.out.println(); System.out.println(); System.out.print("Enter the radius "); double r = scan.nextDouble(); System.out.println(); System.out.println(); double a = Math.PI*r*r; System.out.println("The area is " + a); System.out.println(); System.out.println(); } }