×

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

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

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.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

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.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 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.