×

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

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.