×

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument.

Syntax:

  1. public static double ulp(double x)
  2. 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

Related Topics

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.