Friday, April 13, 2007

SNIPPET, java.lang.Math

SNIPPET, java.lang.Math

Sometimes it is necessary to take time off from the usual and play with the application programming interface. We’ll be using some of the functionalities here when we start real time coding.

package transkawa;

class MathAPI {
static double container = 2.0098765d;
public static void main(String[] args){
System.out.println("Base log to e is: "+Math.E);
System.out.println("PI is "+Math.PI+" in double
primitive.");

//the absolute value of PI is
System.out.println("PI in absolute values
is:"+Math.abs(Math.PI));
System.out.println("log e in abs value is:
"+Math.abs(Math.E));
System.out.println("Absolute value of our container
"+Math.abs(container));

//the smallest double value that is greater than or equal
//to the double value of container and equal to an integer
System.out.println("The ceiling value of container is:
"+Math.ceil(container));
System.out.println("While the floor is:
"+Math.floor(container));

//raising the floor by 2
System.out.println("Let's raise the floor of container to
2: "+Math.pow(Math.floor(container), 2));

//the cosine assuming container is an angle
System.out.println("The cosine of container is:
"+Math.cos(container));

//we can test for mathematical greater than
System.out.println("The greater of PI and log e is:
"+Math.max(Math.PI, Math.E));

//a random number generator also exists for Math class
//you get random numbers all the time:)
System.out.println("What u see is what u get:
"+Math.random());

//let's do a narrowing primitive conversion on container
System.out.println("double to long gives:
"+Math.round(container));

//a little square root pleae
System.out.println("square root of 2.0098756 is:
"+Math.sqrt(container));

System.out.println(Math.ulp(container));
}

}

No comments: