By using java.lang.Math.abs(double a) Method
DemoMath.java
[java]
package com.SPlessons;
import java.lang.Math;
public class DemoMath {
public static void main(String[] args) {
// get some doubles to find their absolute values
double a = 1234.1874d;
double b = -0123.0d;
// get and print their absolute values
System.out.println("Math.abs(" + a + ")=" + Math.abs(a));
System.out.println("Math.abs(" + b + ")=" + Math.abs(b));
System.out.println("Math.abs(-9999.555d)=" + Math.abs(-5555.666d));
}
}
[/java]
The functionality of this method is to return absolute value. Here if the value is zero( either negative or positive) then it returns positive zero.
Output: Following is the result will be generated.
[java]
Math.abs(1234.1874)=1234.1874
Math.abs(-123.0)=123.0
Math.abs(-9999.555d)=5555.666
[/java]
By using Java.lang.Math.acos() Method
DemoMath.java
[java]
package com.SPlessons;
import java.lang.Math;
public class DemoMath {
public static void main(String[] args) {
// get a variable d which is equal to PI/2
double d = Math.PI / 2;
// convert d to radians
d = Math.toRadians(d);
// get the arc cosine of x
System.out.println("Math.acos(" + d + ") = " + Math.acos(d));
}
}
[/java]
Here if the value is NaN(Not a Number) then the result is NaN, here the purpose of j
ava.lang.Math.toRadians
is to change over a point measured in degrees to a roughly proportional edge measured in radians.
Output: The result will be as follows.
[java]
Math.acos(0.027415567780803774) = 1.5433773235341761
[/java]
By using Java.lang.Math.ceil() Method
DemoMath.java
[java]
package com.SPlessons;
import java.lang.Math;
public class DemoMath {
public static void main(String[] args) {
// get two double numbers
double m = 265.9;
double n = 0.5878;
// call ceal for these these numbers
System.out.println("Math.ceil(" + m + ")=" + Math.ceil(m));
System.out.println("Math.ceil(" + n + ")=" + Math.ceil(n));
System.out.println("Math.ceil(-0.65)=" + Math.ceil(-0.95));
}
}
[/java]
In case the argument value is same as integer value then result will be as argument value only.
Output: The result will be as follows.
[java]
Math.ceil(265.9)=266.0
Math.ceil(0.5878)=1.0
Math.ceil(-0.65)=-0.0
[/java]
By using Java.lang.Math.floor() Method
Instead of the
ceil
place the
floor
in the same code then result will be as follows.
Output: The functionality of floor() method is that to return the greatest double value.
[java]
Math.ceil(265.9)=265.0
Math.ceil(0.5878)=0.0
Math.ceil(-0.65)=-1.0
[/java]
By using Java.lang.Math.sqrt() Method
DemoMath.java
[java]
package com.SPlessons;
import java.lang.Math;
public class DemoMath {
public static void main(String[] args) {
// get two double numbers numbers
double m = 16;
double n = 144;
// print the square root of these doubles
System.out.println("Math.sqrt(" + m + ")=" + Math.sqrt(m));
System.out.println("Math.sqrt(" + n + ")=" + Math.sqrt(n));
}
}
[/java]
Here if the argument is negative or positive then the result will be same as the argument. If the argument is less than zero then result will be NaN(Not a Number).
Output: The result will be as follows.
[java]
Math.sqrt(16.0)=4.0
Math.sqrt(144.0)=12.0
[/java]