Package org.gbif.utils.number
Class DoubleVsBigDecimal
java.lang.Object
org.gbif.utils.number.DoubleVsBigDecimal
Set of tests to illustrate the issue with double and how some problems can be solved with
BigDecimal.
BigDecimal is specially useful to work with base 10 type of data (money, metric measures).
Be aware that BigDecimal requires more memory to work with (it's an Object) and it's also slower on
computations (it's an immutable Object).-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidExample from https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems Simply demonstrates that (a + b) + c is not necessarily equal to a + (b + c) using double.voidDemonstrates the effect of Double approximations on equality.voidDemonstrates that the sum of 1/10 (10 times) is not exactly equals to 1d using double.voidDemonstrates the possible issue with the default rounding mode of NumberFormat (RoundingMode.HALF_EVEN).
-
Constructor Details
-
DoubleVsBigDecimal
public DoubleVsBigDecimal()
-
-
Method Details
-
testOneTenthAdditions
Demonstrates that the sum of 1/10 (10 times) is not exactly equals to 1d using double. -
testDoubleApproximation
Demonstrates the effect of Double approximations on equality. -
testRoundingUsingFormat
Demonstrates the possible issue with the default rounding mode of NumberFormat (RoundingMode.HALF_EVEN). This is a modified version from the blog post: https://blogs.oracle.com/CoreJavaTechTips/entry/the_need_for_bigdecimal No idea why the blog says "why does 90.045 round down to 90.04 and not up to 90.05" because it is not the case. -
testAdditionAssociativity
Example from https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems Simply demonstrates that (a + b) + c is not necessarily equal to a + (b + c) using double.
-