Java.math.BigDecimal.ulp() Method



Description

The java.math.BigDecimal.ulp() returns the size of an ulp, a unit in the last place, of this BigDecimal. An ulp of a nonzero BigDecimal value is the positive distance between this value and the BigDecimal value next larger in magnitude with the same number of digits.

An ulp of a zero value is numerically equal to 1 with the scale of this. The result is stored with the same scale as this so the result for zero and nonzero values is equal to [1, this.scale()].

Declaration

Following is the declaration for java.math.BigDecimal.ulp() method.

public BigDecimal ulp()

Parameters

NA

Return Value

This method returns the size of an ulp of BigDecimal.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.ulp() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 4 BigDecimal objects
      BigDecimal bg1, bg2, bg3, bg4;

      bg1 = new BigDecimal("123");
      bg2 = new BigDecimal("1.23");

      // assign ulp value of bg1, bg2 to bg3, bg4
      bg3 = bg1.ulp();
      bg4 = bg2.ulp();

      String str1 = "ULP value of " + bg1 + " is " + bg3;
      String str2 = "ULP value of " + bg2 + " is " + bg4;

      // print bg3, bg4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result −

ULP value of 123 is 1
ULP value of 1.23 is 0.01
java_math_bigdecimal.htm
Advertisements