Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Java Android multiplication problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 12954
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://developer.android.com/reference/java/math/package-summary.html
 
Marshal
Posts: 71681
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1043
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic