posted 18 years ago
I read somewhere that doing floating-point arithmetic is like moving a pile of sand: every time you do it, you lose a little sand and you pick up a little dirt.
If your values are within a decent limited range, you could do something like the following, to use integer rather than floating arithmetic:
This prints out
c = 0.38515595199999997, cNormalized = 0.385155952
For more precise inputs, you need to multiply by something bigger than a million.
If this trick doesn't work, check out java.math.BigDecimal.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".