HelloMath
//
// HelloMath.java
//
public class HelloMath
{
public static void main(String[] args)
{
// The Math library defines common mathematical constants
// and functions. They are all defined as static members
// and methods of the Math class.
System.out.println("pi: " + Math.PI);
System.out.println("e: " + Math.E);
System.out.println("cos(0): " + Math.cos(0));
System.out.println("sin(0): " + Math.sin(0));
System.out.println("cos(pi): " + Math.cos(Math.PI));
System.out.println("sin(pi): " + Math.sin(Math.PI));
// Math.abs() is useful for fuzzy unit tests, which allow
// for roundoff error in floating point calculations.
double result = Math.sin(Math.PI);
if (Math.abs(result) < 1e-6)
System.out.println("Yippee!");
else
System.out.println("Bummer.");
}
}
Output:
pi: 3.141592653589793
e: 2.718281828459045
cos(0): 1.0
sin(0): 0.0
cos(pi): -1.0
sin(pi): 1.2246467991473532E-16
Yippee!