Java Math ulp() Method
The ulp() method of Java Math class returns the size of an ulp of the argument.
Syntax:
- public static double ulp(double x)
- public static float ulp(float x)
Parameters:
The parameter ‘x’ represents the floating-point number whose ulp is to be determined.
Return Value:
The ulp() method returns the size of an ulp of the argument.
- It returns zero with same sign as argument, if the argument passed is zero.
- It returns positive infinity, if the argument is positive or negative infinity.
- It returns Double.MIN_VALUE, if the double at argument is negative or positive zero.
- It returns Float.MIN_VALUE, if the float argument is negative or positive zero.
- It returns a value equal to 2104, if the argument is ±MAX_VALUE.
- It returns a value equal to 2971, if the argument is ±MAX_VALUE
Example 1:
public class JavaMathUlpExample1 { public static void main(String[] args) { double a=90; //returns the size of an ulp of the argument System.out.println("Ulp value for "+a+" : "+Math.ulp(a)); } }
Output:
Ulp value for 90.0 : 1.4210854715202004E-14
Example 2:
public class JavaMathUlpExample2 { public static void main(String[] args) { double a=Double.NEGATIVE_INFINITY; //returns positive infinity, if the argument is positive or negative infinity System.out.println("Ulp value for "+a+" : "+Math.ulp(a)); } }
Output:
Ulp value for -Infinity : Infinity
Example 3:
public class JavaMathUlpExample3 { public static void main(String[] args) { Float a=0f; // returns Float.MIN_VALUE, if the float argument is negative or positive zero System.out.println("Float.MIN_VALUE : "+Float.MIN_VALUE); System.out.println("Ulp value for "+a+" : "+Math.ulp(a)); } }
Output:
Float.MIN_VALUE : 1.4E-45 Ulp value for 0.0 : 1.4E-45
Example 4:
public class JavaMathUlpExample4 { public static void main(String[] args) { Double a=Double.MAX_VALUE; //returns a value equal to 2 raise to power 971, if the argument is ±Double.MAX_VALUE System.out.println("2 raise to power 971 : "+Math.pow(2,971)); System.out.println("Ulp value for "+a+" : "+Math.ulp(a)); } }
Output:
2 raise to power 971 : 1.9958403095347198E292 Ulp value for 1.7976931348623157E308 : 1.9958403095347198E292
Example 5:
public class JavaMathUlpExample5 { public static void main(String[] args) { Float a=Float.MAX_VALUE; //returns a value equal to 2 raise to power 104, if the argument is ±Float.MAX_VALUE System.out.println("2 raise to power 971 : "+Math.pow(2,104)); System.out.println("Ulp value for "+a+" : "+Math.ulp(a)); } }
Output:
2 raise to power 971 : 2.028240960365167E31 Ulp value for 3.4028235E38 : 2.028241E31