Java bigdecimal ulp example

Example below demonstrates working of bigdecimal class ulp method. Method returns bigdecimal values.

Java bigdecimal ulp example

Example below demonstrates working of bigdecimal class ulp method. Method returns bigdecimal values.

Java bigdecimal ulp example

Java bigdecimal ulp example

     

Example below demonstrates working of bigdecimal class ulp method. Method returns  bigdecimal values.  Method returns the size of an ulp. which is the size of a unit at the last place of the bigdecimal object value. 

Method returns +1 for a positive bigdecimal value. Actually this ulp size is the difference between the bigdecimal value and  just the next number after the bigdecimal value. For example,  if the bigdecimal value is 4 then its next value is 5, and there difference that is one, is the value that the ulp method generates.

The scale of the result will be same as that of the this.object value.

Method represents the bigdecimal object value in scientific notations if required. For example if method generates 5E-5, then it means 5 * ten to the power minus 5.

Method generates NumberFormatException, if it finds the bigdecimal value other than integers and double types values. 

Syntax for using the method: public BigDecimal ulp() 
Suppose we have bigdecimal objects x & y;
  then y = x.ulp();
System.out.println(y); 

Java_bigdecimal_ulp.java

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

public class Java_bigdecimal_ulp {
  public static void main(String args[]) {

  BigDecimal nc = new BigDecimal(0);

  System.out.println(" BigDecimal value :" + nc
  "\nmethod generating result :" + nc.ulp());

  nc = new BigDecimal(2);
  System.out.println("\n BigDecimal value :" + nc
  "\nmethod generating result :" + nc.ulp());

  nc = new BigDecimal(3.1);
  System.out.println("\n BigDecimal value :" + nc
  "\nmethod generating result :" + nc.ulp());

  nc = new BigDecimal(4000);
  System.out.println("\n BigDecimal value :" + nc
  "\nmethod generating result :" + nc.ulp());

  }
}

Download the code