Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Android multiplication problem

 
Jack Deli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50205
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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
Rancher
Posts: 981
5
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic