Home »
Java programming language
Java Math class ulp() method with example
Math class ulp() method in Java: Here, we are going to learn about the ulp() method of Math class in Java programming language with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019
Math class ulp() method
- ulp() method is available in java.lang package.
- ulp() method is used to return the size of a ulp of the given argument, where, a ulp of the given value is the positive distance between floating-point value and the value next larger in magnitude.
- ulp() method is a static method, it is accessible with the class name too.
- ulp() method does not throw any exception.
Syntax:
public static float ulp(float value);
public static double ulp(double value);
Parameter(s):
- value – represents the float/double floating-point value whose ulp is to be returned.
Return value:
The return type of this method is float/double – it returns the size of a ulp.
Note:
- If we pass "NaN", it returns the same value (i.e. "NaN").
- If we pass an infinity (+ve or –ve), it returns the infinity.
- If we pass a zero (0 or -0), it returns the "Float.MIN_VALUE" / "Double.MIN_VALUE".
- If we pass "Float.MAX_VALUE", it returns the 2^104 and in the case of "Double.MAX_VALUE", it returns the 2^971.
Java program to demonstrate example of ulp() method
// Java program to demonstrate the example of
// ulp(float fl) method of Math Class
public class UlpFloatTypeMethod {
public static void main(String[] args) {
// declaring the variables
float f1 = 0.0f;
float f2 = -0.0f;
float f3 = 7.0f / 0.0f;
float f4 = -7.0f / 0.0f;
float f5 = 1285.45f;
// Display the values
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("f3: " + f3);
System.out.println("f4: " + f4);
System.out.println("f5: " + f5);
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (0.0)
System.out.println("Math.ulp(f1): " + Math.ulp(f1));
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (-0.0)
System.out.println("Math.ulp(f2): " + Math.ulp(f2));
// Here , we will get (Infinity) because
// we are passing parameter (7.0/0.0)
System.out.println("Math.ulp(f3): " + Math.ulp(f3));
// Here , we will get (Infinity) because
// we are passing parameter (-7.0/0.0)
System.out.println("Math.ulp(f4): " + Math.ulp(f4));
// Here , we will get (2 raised to the power of 104)
// because we are passing parameter (1285.45)
System.out.println("Math.ulp(f5): " + Math.ulp(f5));
}
}
Output
E:\Programs>javac UlpFloatTypeMethod.java
E:\Programs>java UlpFloatTypeMethod
f1: 0.0
f2: -0.0
f3: Infinity
f4: -Infinity
f5: 1285.45
Math.ulp(f1): 1.4E-45
Math.ulp(f2): 1.4E-45
Math.ulp(f3): Infinity
Math.ulp(f4): Infinity
Math.ulp(f5): 1.2207031E-4
Example 2:
// Java program to demonstrate the example of
// ulp(float fl) method of Math Class
public class UlpFloatTypeMethod {
public static void main(String[] args) {
// declaring the variables
double d1 = 0.0;
double d2 = -0.0;
double d3 = 7.0 / 0.0;
double d4 = -7.0f / 0.0;
double d5 = 1285.45;
// Display the values
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
System.out.println("d5: " + d5);
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (0.0)
System.out.println("Math.ulp(d1): " + Math.ulp(d1));
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (-0.0)
System.out.println("Math.ulp(d2): " + Math.ulp(d2));
// Here , we will get (Infinity) because
// we are passing parameter (7.0/0.0)
System.out.println("Math.ulp(d3): " + Math.ulp(d3));
// Here , we will get (Infinity) because
// we are passing parameter (-7.0/0.0)
System.out.println("Math.ulp(d4): " + Math.ulp(d4));
// Here , we will get (2 raised to the power of 971)
// because we are passing parameter (1285.45)
System.out.println("Math.ulp(d5): " + Math.ulp(d5));
}
}
Output
E:\Programs>javac UlpMethod.java
E:\Programs>java UlpMethod
d1: 0.0
d2: -0.0
d3: Infinity
d4: -Infinity
d5: 1285.45
Math.ulp(d1): 4.9E-324
Math.ulp(d2): 4.9E-324
Math.ulp(d3): Infinity
Math.ulp(d4): Infinity
Math.ulp(d5): 2.2737367544323206E-13