Skip to content

Instantly share code, notes, and snippets.

@rmg007
Created October 21, 2022 11:58
Show Gist options
  • Save rmg007/e6be49fbfe89d2b9806954f28fee55b1 to your computer and use it in GitHub Desktop.
Save rmg007/e6be49fbfe89d2b9806954f28fee55b1 to your computer and use it in GitHub Desktop.
Math Method Examples
public class MathMethodsExample {
public static void main(String[] args) {
System.out.println("Max of 6 and 9: " + Math.max(6, 9));
System.out.println("Min of 6 and 9: " + Math.min(6, 9));
System.out.println("The PI value: " + Math.PI);
System.out.println("Square root of 36: " + Math.sqrt(36));
System.out.println("Cube root of 125: " + Math.cbrt(125));
System.out.println("10 raised to the power of 4: " + Math.pow(10, 4));
System.out.println("Absolute value of -12: " + Math.abs(-12));
System.out.println("Absolute value of 9: " + Math.abs(9));
System.out.println("AddExact(6, 4): " + Math.addExact(6, 4));
System.out.println("Decrementing 5: " + Math.decrementExact(5));
System.out.println("Ceil of 9.6: " + Math.ceil(9.6));
System.out.println("Ceil of 9.2: " + Math.ceil(9.2));
System.out.println("floor of 9.7: " + Math.floor(9.7));
System.out.println("floor of 9.2: " + Math.floor(9.2));
System.out.println("round of 4.7: " + Math.round(4.7));
System.out.println("round of 4.5: " + Math.round(4.5));
System.out.println("round of 4.3: " + Math.round(4.3));
System.out.println("Copy Sign from -4 to 3: " + Math.copySign(3, -4));
/*
returns random value between 0.0 and 1.0
that means the min random value could be: 0.0
and the max random value could be 0.9999999
Math.random will never return 1
*/
System.out.println(Math.random());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment