Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example passes a double value to the BigDecimal constructor. Because the decimal literal 0.1 cannot be precisely represented by a double, precision of the BigDecimal is affected.

Code Block
bgColor#FFcccc

// prints 0.1000000000000000055511151231257827021181583404541015625
// when run in FP-strict mode 
System.out.println(new BigDecimal(0.1)); 

...

This compliant solution passes the decimal literal as a String so that the BigDecimal(String val) constructor is invoked and the precision is preserved.

Code Block
bgColor#ccccff

// prints 0.1
// when run in FP-strict mode 
System.out.println(new BigDecimal("0.1"));

...

Automated detection is straightforward.

Bibliography