Friday, August 28, 2009

Java : Simple program using Math class

Math class is present in java.lang package so no need to import, its there by default.
Math class provides all trigonometric functions like sin(), cos(), tan() etc. Also other functions like abs() for calculating absolute values, sqrt() for getting square root, pow(x,y) for calculating power of values etc and many other features. you can solve complex number problems. Also PI value by using Math.PI

lets see a simple example.



/* TestMathClass.java */


public class TestMathClass
{
public static void main(String[] args)
{
int angles[] = { 0, 30, 45, 60, 90, 180 };

System.out.println("----Trigonometric Values----");

for (int i=0;i< angles.length ;i++ )
{
double rad = Math.toRadians(angles[i]);

System.out.println("----Angle: " + angles[i]);
System.out.println("Sin: " + Math.sin(rad));
System.out.println("Cos: " + Math.cos(rad));
System.out.println("Tan: " + Math.tan(rad));
}

System.out.println("----Absolute Values----");

int i = 8;
int j = -5;
System.out.println("Absolute value of " + i + " is :" + Math.abs(i));
System.out.println("Absolute value of " + j + " is :" + Math.abs(j));

float f1 = 1.40f;
float f2 = -5.28f;
System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));
System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));

double d1 = 3.324;
double d2 = -9.324;
System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));
System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));

long l1 = 3L;
long l2 = -4L;
System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));
System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));


System.out.println("----Other Values----");

double x = 11.635;
double y = 2.76;

System.out.println("The value of e is " + Math.E);
System.out.println("exp(" + x + ") is " + Math.exp(x));
System.out.println("log(" + x + ") is " + Math.log(x));
System.out.println("pow(" + x + ", " + y + ") is " + Math.pow(x, y));
System.out.println("sqrt(" + x + ") is " + Math.sqrt(x));



System.out.println("The value of PI is : "+Math.PI);
}
}



/********OUTPUT*********/


----Trigonometric Values----
----Angle: 0
Sin: 0.0
Cos: 1.0
Tan: 0.0
----Angle: 30
Sin: 0.49999999999999994
Cos: 0.8660254037844387
Tan: 0.5773502691896257
----Angle: 45
Sin: 0.7071067811865475
Cos: 0.7071067811865476
Tan: 0.9999999999999999
----Angle: 60
Sin: 0.8660254037844386
Cos: 0.5000000000000001
Tan: 1.7320508075688767
----Angle: 90
Sin: 1.0
Cos: 6.123233995736766E-17
Tan: 1.633123935319537E16
----Angle: 180
Sin: 1.2246467991473532E-16
Cos: -1.0
Tan: -1.2246467991473532E-16
----Absolute Values----
Absolute value of 8 is :8
Absolute value of -5 is :5
Absolute value of 1.4 is :1.4
Absolute value of -5.28 is :5.28
Absolute value of 3.324 is :3.324
Absolute value of -9.324 is :9.324
Absolute value of 3 is :3
Absolute value of -4 is :4
----Other Values----
The value of e is 2.718281828459045
exp(11.635) is 112983.8311174165
log(11.635) is 2.454017796754255
pow(11.635, 2.76) is 874.0076417529531
sqrt(11.635) is 3.411011580162108
The value of PI is : 3.141592653589793

No comments:

Post a Comment