Java ulp Function

The Java ulp Function is one of the Math functions, which is to return the size of an ulp of the argument. An ulp of a float value is the positive distance between the given value and the next value that is larger in magnitude. In this article, we will show how to use Math.ulp function with examples.

Java ulp Syntax

The basic syntax of the Math.ulp Function in Java Programming language is as shown below.

Math.ulp(data_type number);

Number: It can be a number or a valid numerical expression.

  • If the number argument is positive or negative double or float value, Math.ulp function will return the output.
  • If the number argument is not a number, Math.ulp function will return NaN.
  • When the number argument is positive or negative Zero, the Java Math.ulp function will return Float.MIN_EXPONENT.
  • If the number argument is positive or negative Infinity, Math.ulp function will return Positive Infinity.
  • If it is positive or negative Float.MAX_EXPONENT, Math.ulp function will return the result as 2104.

Java Programming provides two different functions to find the ulp of the specified value. The following function will accept positive or negative float value as an argument and returns the float ulp.

static float ulp(float number); //Return Type is Float

// In order to use in program: 
Math.ulp(float number);

The following function will accept positive or negative double value as an argument and returns the ulp of type double.

static double ulp(double number); //Return Type is Double

// In order to use in program: 
Math.ulp(double number);

Java ulp Function Example

In this program, We use the Java Math.ulp function to find both positive and negative values and display the output.

//Java Math.ulp Function example
package MathFunctions;

public class UlpMethod {
	public static void main(String[] args) {
		double a = 2.352, b = -6.953;
		float c = 65.14f, d = -16.590f;	

		System.out.println("\nMath.ulp Function result of Zero: " + Math.ulp(0));
		
		System.out.println("\nMath.ulp Function of result Positive Double Value: " + Math.ulp(a));
		System.out.println("Math.ulp Function of result Negative Double Value: " + Math.ulp(b));
		
		System.out.println("\nMath.ulp Function of result Positive Float Value: " + Math.ulp(c));
		System.out.println("Math.ulp Function of result Negative Float Value: " + Math.ulp(d));
		
		double e = Math.ulp(100.90 + 165.10 - 180.50 + 6.50);
		System.out.println("\nMath.ulp Function results = " + e);
	}
}
Java ulp Function 1

First, We used the Zero as the argument of Math.ulp Function. Next, We used the Java Math.ulp Function on variables a and b (they belong to double type). The following statements will call the ulp method of double type ( static double ulp(double number) ) to display the result.

System.out.println("\nMath.ulp Function result of Zero: " + Math.ulp(0));

System.out.println("\nMath.ulp Function of result Positive Double Value: " + Math.ulp(a));
System.out.println("Math.ulp Function of result Negative Double Value: " + Math.ulp(b));

Then, We used the scalb Math function on variables c and d (they belong to float type). The following statements will call the ulp method of float type ( static float ulp(float number) ) to display the result.

System.out.println("\nMath.ulp Function of result Positive Float Value: " + Math.ulp(c));
System.out.println("Math.ulp Function of result Negative Float Value: " + Math.ulp(d));

Lastly, We declared a variable of type Double and performed the Math.ulp function directly on the expression.

double e = Math.ulp(100.90 + 165.10 - 180.50 + 6.50);
System.out.println("\nMath.ulp Function results = " + e);