Math
Functions
- The class Math from package java.lang contains methods for performing basic
numeric operations such as the elementary exponential, logarithm, square root,
and trigonometric functions.
Some
Useful Mathematic Function
Math.abs(double) - Returns
the absolute value of a double value.
Math.sqrt(double) -
Returns the square root of a double value.
Math.pow(double,
double) - Returns of value of the first argument raised to the power of the
second argument.
Math.min(double,
double) – Returns the smaller of two double values.
Math.max(double,
double) – Returns the larger of two double values.
Math.sin(double) /
Math.cos(double) / Math.tan(double) - Returns the trigonometric sine, cosine
and tangent of an angle.
Math.exp(double) -
Returns the exponential number e (i.e., 2.718...) raised to the power of a
double value.
Math.ceil(double) -
Returns the smallest (closest to
negative infinity) double value that is not less than the argument and is equal
to a mathematical integer. Math. ceil(12.5) = 13, Math.ceil(-12.5) = -12
Math.floor(double) -
Returns the largest (closest to positive infinity) double value that is not
greater than the argument and is equal to a mathematical integer.
Math.floor(12.5) = 12, Math.floor(-12.5) = -13.
Math.random(double)
- Returns a random number between 0.0 and 1.0.
Math.round(double) -
Returns the closest long to the argument.
Math.rint(double) -
Returns the closest double value that is equal to a mathematical integer.. e.g.
double d=56.678; will give 57.0 and double d=56.478 will give 56.0.
Example
:
import
java.io.*;
class Math_Functions
{
public static void main(String
args[])throws IOException
{
Math_Functions MF=new
Math_Functions();
MF.Sqrt();
MF.Power();
MF.Trigonometric_Fun();
MF.Max_Min();
MF.Ceil_Floor();
MF.Round();
MF.Rint();
MF.ABS();
MF.Random();
}
void Sqrt()
{
double num=12,sq=0;
sq=Math.sqrt(num);
System.out.println("\n\tSquare Root '"+num+"' =
"+sq);
}
void Power()
{
double x=5,y=3.5,pw;
pw=Math.pow(x,y);
System.out.println("\n\t"+x+"^"+y+" =
"+pw);
}
void Trigonometric_Fun()
{
System.out.println("\n\tTrignometic Functions : ");
double
Ang=90,Rad=0.0,Result=0.0;
Rad=Math.toRadians(Ang);
System.out.println("\n\t Radian of
"+Ang+" = "+Rad);
Result=Math.sin(Rad);
System.out.println("\n\t Sine of "+Ang+" =
"+Result);
Result=Math.cos(Rad);
System.out.println("\n\t
Cosine of "+Ang+" = "+Result);
Result=Math.tan(Rad);
System.out.println("\n\t Tan of "+Ang+" = "+Result);
}
void Max_Min()
{
int a=15,b=35;
System.out.println("\n\tMaximum
of "+a+" and " + b+" = "+Math.max(a,b));
System.out.println("\n\tMinimum of "+a+" and " +
b+" = "+Math.min(a,b));
}
void Ceil_Floor()
{
double a=15.5,b=-35.5;
System.out.println("\n\tFloor "+a+" is =
"+Math.floor(a));
System.out.println("\n\tCeil "+a+" is =
"+Math.ceil(a));
System.out.println("\n\tFloor
"+b+" is = "+Math.floor(b));
System.out.println("\n\tCeil "+b+" is =
"+Math.ceil(a));
}
void Round()
{
double x=145.5,y=0.0;
y=Math.round(x);
System.out.println("\n\tRounded value of "+x+" to
"+y);
}
void Rint()
{
double x=15.323,y=0.0;
y=Math.rint(x);
System.out.println("\n\tRint of
"+x+" = "+y);
x=15.5;
y=Math.rint(x);
System.out.println("\n\tRint of "+x+" = "+y);
}
void ABS()
{
int a=-15,b=35;
System.out.println("\n\tAbsoulute Value of "+a+" =
"+Math.abs(a));
System.out.println("\n\tAbsoulute Value of "+b+" =
"+Math.abs(b));
}
void Random()
{
int a=2;
System.out.println("\n\tRandom Nos. in a range of 100\n\t");
System.out.print("\t");
for(int i=1; i<=10;i++)
{
System.out.print((int)(Math.random()*100)+"
");
}
}
}
Output:
Square Root '12.0' = 3.4641016151377544
5.0^3.5 =
279.5084971874737
Trignometic Functions :
Radian
of 90.0 = 1.5707963267948966
Sine
of 90.0 = 1.0
Cosine
of 90.0 = 6.123233995736766E-17
Tan
of 90.0 = 1.633123935319537E16
Maximum of 15 and 35 = 35
Minimum of 15 and 35 = 15
Floor 15.5 is = 15.0
Ceil 15.5 is = 16.0
Floor -35.5 is = -36.0
Ceil -35.5 is = 16.0
Rounded value of 145.5 to 146.0
Rint of 15.323 = 15.0
Rint of 15.5 = 16.0
Absoulute Value of -15 = 15
Absoulute Value of 35 = 35
Random Nos. in a range of 100
10 44 46 64 79 20 49 60 45 51
No comments:
Post a Comment