×

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

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.