Close

Java - Math.ulp() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static double ulp(double d)

Returns the size of an ulp of the argument. An ulp, unit in the last place, of a double value is the positive distance between this floating-point value and the double value next larger in magnitude. .

Parameters:
d - the floating-point value whose ulp is to be returned
Returns:
the size of an ulp of the argument
Since:
1.5



public static float ulp(float f)

Returns the size of an ulp of the argument. An ulp, unit in the last place, of a float value is the positive distance between this floating-point value and the float value next larger in magnitude.

Parameters:
f - the floating-point value whose ulp is to be returned
Returns:
the size of an ulp of the argument
Since:
1.5


Examples


package com.logicbig.example.math;

public class UlpExample {

public static void main(String... args) {
findLeastPrecision(43.435);
findLeastPrecision(23.521);
findLeastPrecision(Double.MAX_VALUE);
findLeastPrecision(0.0);
findLeastPrecision(0.0 / 0.0);
}

private static void findLeastPrecision(double value) {
double result = Math.ulp(value);
System.out.printf("Math.ulp(%s) = %s %n", value, result);
}
}

Output

Math.ulp(43.435) = 7.105427357601002E-15 
Math.ulp(23.521) = 3.552713678800501E-15
Math.ulp(1.7976931348623157E308) = 1.9958403095347198E292
Math.ulp(0.0) = 4.9E-324
Math.ulp(NaN) = NaN




package com.logicbig.example.math;

public class UlpExample2 {

public static void main(String... args) {
findLeastPrecision(24.395f);
findLeastPrecision(-1.25f);
findLeastPrecision(Float.MAX_VALUE);
findLeastPrecision(0.0f);
findLeastPrecision(0.0f / 0);
findLeastPrecision(-1.0f / 0);
}

private static void findLeastPrecision(float value) {
float result = Math.ulp(value);
System.out.printf("Math.ulp(%s) = %s %n", value, result);
}
}

Output

Math.ulp(24.395) = 1.9073486E-6 
Math.ulp(-1.25) = 1.1920929E-7
Math.ulp(3.4028235E38) = 2.028241E31
Math.ulp(0.0) = 1.4E-45
Math.ulp(NaN) = NaN
Math.ulp(-Infinity) = Infinity




See Also