resources.report.rules.pmd.AvoidDecimalLiteralsInBigDecimalConstructor.html Maven / Gradle / Ivy
AvoidDecimalLiteralsInBigDecimalConstructor
AvoidDecimalLiteralsInBigDecimalConstructor
One might assume that the result of “new BigDecimal(0.1)” is exactly equal to 0.1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: ‘new BigDecimal(“0.1”)’ is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
//AllocationExpression
[ClassOrInterfaceType[@Image="BigDecimal"]]
[Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix
[
Literal[(not(ends-with(@Image,'"'))) and contains(@Image,".")]
or
Name[ancestor::Block/BlockStatement/LocalVariableDeclaration
[Type[PrimitiveType[@Image='double' or @Image='float']
or ReferenceType/ClassOrInterfaceType[@Image='Double' or @Image='Float']]]
/VariableDeclarator/VariableDeclaratorId/@Image = @Image
]
or
Name[ancestor::MethodDeclaration/MethodDeclarator/FormalParameters/FormalParameter
[Type[PrimitiveType[@Image='double' or @Image='float']
or ReferenceType/ClassOrInterfaceType[@Image='Double' or @Image='Float']]]
/VariableDeclaratorId/@Image = @Image
]
]
]
Example(s):
BigDecimal bd = new BigDecimal(1.123); // loss of precision, this would trigger the rule
BigDecimal bd = new BigDecimal("1.123"); // preferred approach
BigDecimal bd = new BigDecimal(12); // preferred approach, ok for integer values