This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrong value

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am running this formula in my java code and the output I am getting is: 8.28124999171875E8. If i put the same formula in an excel file or use calculator the output i am getting is: 828124999.17 which i believe is the correct value. What is the mistake i am making while computing the formula in my java code?
Double value = ((2500/144) * (999999999/1000)) * 47.7

All the values in the formulas are variable of type double.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use BigDecimal insteadof Double , if you want to eliminate the E .
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are the same value (more or less).
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:Use BigDecimal insteadof Double , if you want to eliminate the E .


Changing the data structure because you don't like how it is automatically formatted when you print it -- without formatting -- is a bit silly, isn't it? Would it not be better to format it? Maybe either use the printf() method (java.io.PrintStream) or the java.text.DecimalFormat class?

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David is right. The E8 means "times 10 to the power 8", or 100,000,000. So 8.28124999171875E8 is actually 828124999.171875 - which is roughly the same as 828124999.17. That 0.001875 may be caused by rounding issues, something double and float are notorious for - not just in Java but in just about any programming language.
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Sharma wrote:Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.


When formatting at the end, wonder what kind of rounding (RoundingMode) OP used or others suggest to use here. Because using different rounding methods and scale, together, would return different results, anyways. What I meant to say is that this calculation might have matched the result from using double data types and formatting at the end with a specific rounding might not match for another calculation. In complex and lengthier or looped calculations, the errors just propagate.

Whereas BigDecimal pretty much matches the results with those from the calculator. Here is an example quoted from the web, that can be solved by both BigDecimal and formatting.



The above problem can be solved by using BigDecimal as follows:



But because calculations are a pain and performance worse you could always do something like this:

which will also give the result same as the BigDecimal.

Not only is the choice of data type tricky but choice of roundingMode and scale are also tricky to just get calculator kind calculations. Could not come across any recently written articles when I googled about the best practices in using BigDecimal, when to use and how to use for optimal performance, any suggestions?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic