BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.ulp() Method

By Chaitanya Singh | Filed Under: java

Java Math.ulp() method returns distance between the number passed as argument and the next floating point number. An ulp (unit in the last place or unit of least precision) is the distance between two consecutive floating point numbers.

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 0.99;
    System.out.println(Math.ulp(d));
  }
}

Output:

1.1102230246251565E-16

Syntax of Math.ulp() method

public static double ulp(double d)
public static float ulp(float f)

ulp() Description

For argument d: It returns the positive distance between argument d and next floating point double value, which is larger in magnitude.

For argument f: It returns the positive distance between argument f and next floating point float value which is larger in magnitude than the argument.

ulp() Parameters

  • d: A double value whose ulp is to be determined.
  • f: A float value whose ulp is to be determined.

ulp() Return Value

  • Size of an ulp of the argument.
  • For non-NaN argument x, the ulp(-x) == ulp(x).
  • If the argument is NaN (Not a number), then it returns NaN.
  • If the argument is positive or negative infinity, then it returns positive infinity.
  • If the argument d is positive or negative zero, then it returns Double.MIN_VALUE.
  • If the argument f is positive or negative zero, then it returns Float.MIN_VALUE.
  • If the argument d is +or- Double.MAX_VALUE, then it returns 2971.
  • If the argument f is +or- Float.MAX_VALUE, then it returns 2104.

Example 1

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = -5.99;
    double d2 = 5.99;
    System.out.println(Math.ulp(d));
    System.out.println(Math.ulp(d2));
  }
}

Output:

Java Math.ulp() Example Output1

Example 2

public class JavaExample
{
  public static void main(String[] args)
  {
    float f = Float.POSITIVE_INFINITY;
    System.out.println(Math.ulp(f));
  }
}

Output:

Java Math.ulp() Example Output2

Example 3

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = Double.MAX_VALUE;
    float f = Float.MAX_VALUE;
    System.out.println(Math.ulp(d));
    System.out.println(Math.ulp(f));
  }
}

Output:

Java Math.ulp() Example Output3

Example 4

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = -0.0;
    float f = 0.0f;
    System.out.println(Math.ulp(d));
    System.out.println(Math.ulp(f));
  }
}

Output:

Java Math.ulp() Example Output4

Recommended Posts

  • Java Math.IEEEremainder()
  • Java Math.getExponent()
  • Java Math.hypot()
  • Java Math.rint()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap