aspose file tools*
The moose likes Beginning Java and the fly likes Java Android multiplication problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Android multiplication problem" Watch "Java Android multiplication problem" New topic
Author

Java Android multiplication problem

Jack Deli
Greenhorn

Joined: Jul 16, 2012
Posts: 2
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

http://developer.android.com/reference/java/math/package-summary.html


[How To Ask Questions][Read before you PM me]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: May 22, 2012
Posts: 905
    
    5
http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8420
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Mar 17, 2011
Posts: 8420
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Android multiplication problem