In the field of programming, we perform various numerical calculations. The value of evaluation and its performance in terms of space and time complexity is very critical. In order to ease the performance part, Java has Math
class. This class comprises various methods that perform basic numeric operations like the elementary exponential, square root, logarithm, and series of trigonometric functions.
Math
is part of java.lang
library (This is a library which is an implicit part of every Java program).
So let us discuss a range of methods that Math
class provides. It must be noted that all the methods of this class are static. So these are class members and can be easily called using the following format:
Math.<methodName>(<series of parameters applicable>)
The list of methods mentioned below are given in alphabetical order and very frequently put to use.
public class MyMath{
public static void main(String []args){
float myFloat=-123.123f;
double myDouble=-121.4354;
int myInt=12;
long myLong=-1213123;
System.out.println("abs() of float "+myFloat+" is "+Math.abs(myFloat));
System.out.println("abs() of double "+myDouble+" is "+Math.abs(myDouble));
System.out.println("abs() of int "+myInt+" is "+Math.abs(myInt));
System.out.println("abs() of long "+myLong+" is "+Math.abs(myLong));
}
}
abs() of float -123.123 is 123.123
abs() of double -121.4354 is 121.4354
abs() of int 12 is 12
abs() of long -1213123 is 1213123
public class MyMath{
public static void main(String []args){
double myDouble=0.9;
System.out.println("acos("+myDouble+") = "+Math.acos(myDouble));
}
}
acos(0.9) = 0.45102681179626236
public class MyMath{
public static void main(String []args){
double myDouble=0.9;
System.out.println("asin("+myDouble+") = "+Math.asin(myDouble));
}
}
asin(0.9) = 1.1197695149986342
public class MyMath{
public static void main(String []args){
double myDouble=0.9;
System.out.println("atan("+myDouble+") = "+Math.atan(myDouble));
}
}
atan(0.9) = 0.7328151017865066
public class MyMath{
public static void main(String []args){
double myDouble=1.7;
double myDouble2=4.6;
System.out.println("atan2("+myDouble+","+myDouble2+") = "+Math.atan2(myDouble,myDouble2));
}
}
atan2(1.7,4.6) = 0.35399743683189616
public class MyMath{
public static void main(String []args){
double myDouble=1743.56;
System.out.println("cbrt("+myDouble+") = "+Math.cbrt(myDouble));
}
}
cbrt(1743.56) = 12.03591094498721
public class MyMath{
public static void main(String []args){
double myDouble=1743.56;
System.out.println("cos("+myDouble+") = "+Math.cos(myDouble));
}
}
cos(1743.56) = -0.9997138648461751
public class MyMath{
public static void main(String []args){
double myDouble=1.56;
System.out.println("cosh("+myDouble+") = "+Math.cosh(myDouble));
}
}
cosh(1.56) = 2.4844786581693095
public class MyMath{
public static void main(String []args){
double myDouble=1.56;
System.out.println("exp("+myDouble+") = "+Math.exp(myDouble));
}
}
exp(1.56) = 4.758821245137854
public class MyMath{
public static void main(String []args){
double myDouble=1.56;
System.out.println("floor("+myDouble+") = "+Math.floor(myDouble));
}
}
floor(1.56) = 1.0
public class MyMath{
public static void main(String []args){
double myDouble=3;
double myDouble2=4;
System.out.println("hypot("+myDouble+","+myDouble2+") = "+Math.hypot(myDouble,myDouble2));
}
}
hypot(3.0,4.0) = 5.0
public class MyMath{
public static void main(String []args){
double myDouble=3;
System.out.println("log("+myDouble+") = "+Math.log(myDouble));
}
}
log(3.0) = 1.0986122886681098
public class MyMath{
public static void main(String []args){
double myDouble=10;
System.out.println("log10("+myDouble+") = "+Math.log10(myDouble));
}
}
log10(10.0) = 1.0
public class MyMath{
public static void main(String []args){
double myDouble=121.435;
double myDouble2=123.123;
int myInt=12;
int myInt2=43;
long myLong=4334332;
long myLong2=2423432;
float myFloat = 123.12f;
float myFloat2 = 154.12f;
System.out.println("Float max("+myFloat+","+myFloat2+") is "+Math.max(myFloat,myFloat2));
System.out.println("Long max("+myLong+","+myLong2+") is "+Math.max(myLong,myLong2));
System.out.println("Int max("+myInt+","+myInt2+") is "+Math.max(myInt,myInt2));
System.out.println("Double max("+myDouble+","+myDouble2+") is "+Math.max(myDouble,myDouble2));
}
}
Float max(123.12,154.12) is 154.12
Long max(4334332,2423432) is 4334332
Int max(12,43) is 43
Double max(121.435,123.123) is 123.123
public class MyMath{
public static void main(String []args){
double myDouble=121.435;
double myDouble2=123.123;
int myInt=12;
int myInt2=43;
long myLong=4334332;
long myLong2=2423432;
float myFloat = 123.12f;
float myFloat2 = 154.12f;
System.out.println("Float min("+myFloat+","+myFloat2+") is "+Math.min(myFloat,myFloat2));
System.out.println("Long min("+myLong+","+myLong2+") is "+Math.min(myLong,myLong2));
System.out.println("Int min("+myInt+","+myInt2+") is "+Math.min(myInt,myInt2));
System.out.println("Double min("+myDouble+","+myDouble2+") is "+Math.min(myDouble,myDouble2));
}
}
Float min(123.12,154.12) is 123.12
Long min(4334332,2423432) is 2423432
Int min(12,43) is 12
Double min(121.435,123.123) is 121.435
public class MyMath{
public static void main(String []args){
double myDouble=2;
double myDouble2=3;
System.out.println("pow("+myDouble+","+myDouble2+") is "+Math.pow(myDouble,myDouble2));
}
}
pow(2.0,3.0) is 8.0
public class MyMath{
public static void main(String []args){
System.out.println("1 random() : "+Math.random());
System.out.println("2 random() : "+Math.random());
System.out.println("3 random() : "+Math.random());
}
}
1 random() : 0.08393546021648257
2 random() : 0.2142775463552603
3 random() : 0.33068510951362495
public class MyMath{
public static void main(String []args){
double myDouble=121.435;
float myFloat = 154.12f;
System.out.println("round("+myDouble+") = "+Math.round(myDouble));
System.out.println("round("+myFloat+") = "+Math.round(myFloat));
}
}
round(121.435) = 121
round(154.12) = 154
public class MyMath{
public static void main(String []args){
double myDouble=62.435;
System.out.println("sin("+myDouble+") = "+Math.sin(myDouble));
}
}
sin(62.435) = -0.3865179060390371
public class MyMath{
public static void main(String []args){
double myDouble=0.5;
System.out.println("sinh("+myDouble+") = "+Math.sinh(myDouble));
}
}
sinh(0.5) = 0.5210953054937474
public class MyMath{
public static void main(String []args){
double myDouble=5;
System.out.println("sqrt("+myDouble+") = "+Math.sqrt(myDouble));
}
}
sqrt(5.0) = 2.23606797749979
public class MyMath{
public static void main(String []args){
double myDouble=1;
System.out.println("tan("+myDouble+") = "+Math.tan(myDouble));
}
}
tan(1.0) = 1.5574077246549023
public class MyMath{
public static void main(String []args){
double myDouble=0.96;
System.out.println("tanh("+myDouble+") = "+Math.tanh(myDouble));
}
}
tanh(0.96) = 0.7442768673618372
public class MyMath{
public static void main(String []args){
double myDouble=1.57;
System.out.println("toDegrees("+myDouble+") = "+Math.toDegrees(myDouble));
}
}
toDegrees(1.57) = 89.95437383553926
public class MyMath{
public static void main(String []args){
double myDouble=90;
System.out.println("toRadians("+myDouble+") = "+Math.toRadians(myDouble));
}
}
toRadians(90.0) = 1.5707963267948966
This was just a glimpse among so many methods that are available to be utilized effectively. The readers are expected to explore more methods available with Math
class. In the next lecture, we will discuss Java Date and Time
.