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!

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.

Ivan Jozsef Balazs wrote:http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf

I wondered when good old Goldberg would pop up. Well done Ivan. +1.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

fred rosenberger wrote:Floating point multiplication is never very accurate.

I'm not sure we can blame the inaccuracy in fp arithmetic for the difference the OP is seeing

Jack Deli wrote:for example 20 Decigmal-force to Pond should be 0.141 863 275 06...yet my program say's that it is 2819.6162808000004

Stuart A. Burkett wrote:I'm not sure we can blame the inaccuracy in fp arithmetic for the difference the OP is seeing

Jack Deli wrote:for example 20 Decigmal-force to Pond should be 0.141 863 275 06...yet my program say's that it is 2819.6162808000004

True. Mind you, we can't work out very much at all from what he's supplied us with.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here