×

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

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

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.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.