I encountered an error when writing a converter class with Java. it doesn't convert with precise accuracy, for example 20 Decigmal-force to Pond should be 0.141 863 275 06...yet my program say's that it is 2819.6162808000004!?! Here is my code below(please ask questions if I need to add extra info!!).
This is the code that does the calculation
next part when the output happens
Please help or at least point me in the right direction to solve this problem!!
Thanks in advance!
Floating point multiplication is never very accurate. This isn't a problem with java, android, perl, c++ or any other language, it's just the nature of the beast.
Ask yourself if you REALLY need that degree of precision. If the answer is yes, ask yourself again a couple more times. If after all that, you still come back with yes, then you have to explore other options, such as BigDecimal (although I am not sure if that is available in the Android platform).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I think it is unfortunate that people are taught to use floating‑point arithmetic before they are taught accurate fractional arithmetic.You will find a bit about it here, and much more if you follow the links in that post.That class would appear to be available in the link Bill Gorder quoted earlier, but there is something confusing; it talks about the Integer class, which the JavaSE version doesn’t.