Java Math ulp() Method
Example
Find the unit of least precision for different numbers:
System.out.println(Math.ulp(1.0));
System.out.println(Math.ulp(1.0f));
System.out.println(Math.ulp(5000000.0));
System.out.println(Math.ulp(5000000.0f));
System.out.println(Math.ulp(50000000.0));
System.out.println(Math.ulp(50000000.0f));
Definition and Usage
The ulp()
method returns the unit of least precision of a number.
The unit of least precision is the smallest step you can take up or down from a number. For example, the ulp for 50000000.0f
is 4.0, so the next number above it than can be represented with a float
data type is 50000004.0f
.
Note: The double
data type has a lot more precision than the float
data type, so the ulp is smaller.
Note: Larger numbers have less precision than smaller numbers, that means the ulp is larger.
Note: The sign of a number does not affect the ulp.
Syntax
public static double ulp(double number)
public static float ulp(float number)
Parameter Values
Parameter | Description |
---|---|
number | Required. A floating point number. |
Technical Details
Returns: | A double or float value representing the unit of least precision. |
---|---|
Java version: | 1.5+ |
❮ Math Methods