Tuesday, November 2, 2021

Java Tutorial: RoundingMode

Chapters

RoundingMode

RoundingMode specifies a rounding policy for numerical operations capable of discarding precision. In other words, we round off numbers to remove least significant digits on the rightmost part of a decimal number.

The digits next to the dot(.) of a decimal number are called fractional digits or just fraction. Numbers that past a certain scale(number of decimal digits) are discarded. There are eight available rounding modes in java that we can use.

RoundingMode.UP

This round mode rounds up a value if a discarded fractional digit is non-zero. Otherwise, the value prior to the discarded fractional digit won't be changed.
Value	Rounded-off
3.3     4
5.0     5
-3.3    -4
-5.0    -5
4.34    4.4
4.50    4.5
-4.34   -4.4
-4.50   -4.5
RoundingMode.DOWN

This round mode rounds down a value prior to a discarded fractional digit to zero. This round mode never increments the value prior to the discarded fractional digit.
Value	Rounded-off
3.3     3
5.6     5
-3.3    -3
-5.6    -5
4.34    4.3
4.50    4.5
-4.34   -4.3
-4.50   -4.5
RoundingMode.CEILING

This rounding mode rounds off a value towards positive infinity. In other words, this round mode rounds off the value towards the right side of a number line. Assuming the right side of the number line is positive.

This rounding mode acts as RoundingMode.UP if the decimal number is positive. If the decimal number is negative then, this mode acts as RoundingMode.DOWN.
Value	Rounded-off
3.1     4
5.6     6
-3.1    -3
-5.6    -5
4.34    4.4
4.50    4.5
-4.34   -4.3
-4.50   -4.5
RoundingMode.FLOOR

This rounding mode is the opposite of RoundingMode.CEILING. This rounding mode rounds off a value towards negative infinity. In other words, this rounding mode rounds off the value towards the left side of a number line. Assuming that left side of the number line is negative.

This mode acts as RoundingMode.UP if the decimal number is negative. If the decimal number is positive then, this mode acts as RoundingMode.DOWN.
Value	Rounded-off
3.1     3
5.6     5
-3.1    -4
-5.6    -6
4.34    4.3
4.50    4.5
-4.34   -4.4
-4.50   -4.5
Brief Explanation of Nearest Neighbor

In the next topic, you will encounter the word "nearest neighbor". Nearest neighbor is a mathematical algorithm that finds a closest point to a given point. For those who don't know this algorithm, you may wanna read this topic. I'm gonna discuss the basic concept of nearest neighbor here.

This nearest neighbor concept is just like this: imagine a line with two endpoints: 0 and 1. let's say 0 is on the left side of the line and 1 is on the right side. Then, there's a point in the middle of both endpoints. If the middle point is in 0.5 then, two endpoints are equidistant to the middle point. In other words, both distances of two endpoints to the middle point are equal.

If the middle point is in 0.6 and above then, middle point is close to 1. Therefore, middle point's nearest neighbor is 1. If the middle point is in 0.4 and below then, middle point is close to 0. Therefore, middle point's nearest neighbor is 0.

RoundingMode.HALF_UP

This rounding mode rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is >= 0.5; otherwise, behaves as for RoundingMode.DOWN. Note that this is the rounding mode is commonly taught at school.
Value	Rounded-off
3.5     4
5.6     6
-3.5    -4
-5.6    -6
4.34    4.3
4.50    4.5
-4.34   -4.3
-4.50   -4.5
RoundingMode.HALF_DOWN

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for RoundingMode.UP if the discarded fraction is > 0.5; otherwise, behaves as for RoundingMode.DOWN.
Value	Rounded-off
3.5     3
5.6     6
-3.5    -3
-5.6    -6
4.34    4.3
4.50    4.5
-4.34   -4.3
-4.50   -4.5
RoundingMode.HALF_EVEN

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it's even.
Value	Rounded-off
5.5     6
6.5     6
-5.5    -6
-6.5    -6
2.5     2
3.5     4
2.4     2
3.4     3
5.54    5.5
5.56    5.6
5.44    5.4
5.46    5.5
Take a look at the first two numbers in the example above. 5.5 is rounded up to 6 but 6.5 is not rounded up to 7. These two numbers are somewhat special because they are equidistant to both of their neighbors.

If a number is equidistant, round the number towards a highest even neighbor. So, 5.5 has two even neighbors which are 4 and 6. 6 is higher than 4. Thus, 5.5 is rounded off to 6. 6.5 has two neighbors which are 5 and 7. However, its neighbors are not even. That's why 6.5 can't be rounded off to 7 because 7 is odd.

RoundingMode.UNNECESSARY

This rounding mode means that all kinds of rounding modes that we have discussed are unnecessary. This mode expects an exact value without other rounding modes involved. Othewise, an ArithmeticException will be thrown.
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class SampleClass{

  public static void main(String[] args){
    
    BigDecimal bd1 = new BigDecimal("2.34");
    BigDecimal bd2 = new BigDecimal("3.334");
    BigDecimal result = null;
    
    //the sum of this operation doesn't involve
    //other rounding modes or operations that
    //similar to those modes.
    result = bd1.add(bd2, new MathContext(4,
                     RoundingMode.UNNECESSARY));
    System.out.println("output1: " + result);
    
    bd2 = new BigDecimal("7.994");
    
    //This will throw an ArithmeticException
    //because the sum of 2.34 and 7.994 would
    //need to involve other rounding modes
    //or operations that are similar to those
    //modes in order to fit into the specified
    //precision
    /*
    result = bd1.add(bd2, new MathContext(4,
                     RoundingMode.UNNECESSARY));
    */
  }
}

No comments:

Post a Comment