BigDecimal ulp() Method in Java
The java.math.BigDecimal.ulp() is an inbuilt method in Java that returns the size of an ulp(unit in the last place) of this BigDecimal.
- An ulp of a nonzero BigDecimal value is defined as 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()].
Syntax:
public BigDecimal ulp()
Parameter: The method does not accept any parameter.
Return value: This method returns the size of an ulp of BigDecimal.
Examples:
Input: 4.25 Output: 0.01 Input: 1789 Output: 1
Below programs illustrates the above mentioned method:
Program 1:
// Program to illustrate the ulp() method of BigDecimal import java.math.*; public class gfg { public static void main(String[] args) { // Assigning BigDecimal object BigDecimal b1 = new BigDecimal( "1789" ); BigDecimal b2 = new BigDecimal( "4.25" ); // Assigning ulp value of BigDecimal object b1, b2 to b3, b4 BigDecimal b3 = b1.ulp(); BigDecimal b4 = b2.ulp(); // Printing b3, b4 values System.out.println( "ULP value of " + b1 + " is " + b3); System.out.println( "ULP value of " + b2 + " is " + b4); } } |
ULP value of 1789 is 1 ULP value of 4.25 is 0.01
Program 2:
// Program to illustrate the ulp() method of BigDecimal import java.math.*; public class gfg { public static void main(String[] args) { // Assigning BigDecimal object BigDecimal b1 = new BigDecimal( "78645" ); BigDecimal b2 = new BigDecimal( "4.252547" ); // Assign ulp value of BigDecimal object b1, b2 to b3, b4 BigDecimal b3 = b1.ulp(); BigDecimal b4 = b2.ulp(); // Printing b3, b4 values System.out.println( "ULP value of " + b1 + " is " + b3); System.out.println( "ULP value of " + b2 + " is " + b4); } } |
ULP value of 78645 is 1 ULP value of 4.252547 is 0.000001
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#ulp()
Recommended Posts:
- BigDecimal plus() method in Java
- BigDecimal max() Method in Java
- BigDecimal min() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal movePointLeft() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal multiply() Method in Java
- BigDecimal scale() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal pow() method in Java with Examples
- BigDecimal add() Method in Java with Examples
- BigDecimal movePointRight() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.