This week's giveaway is in the Spring forum.We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Java 7 - Sum

Ranch Hand
Posts: 301
• Number of slices to send:
Optional 'thank-you' note:
I'm working on Sum and running into a wierd problem... I don't want to compromise what anyone else is working on, so no code, but here are the results of the program...
C:\JavaSource\CattleDrive>java Sum
0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999 is not equal to 100
Can someone who's been there point me in the right direction? I've read what I could find on double and floating point math, but sheesh it shouldn't be doing this!
Thanks!
------------------

Joel Cochran

Ranch Hand
Posts: 169
• Number of slices to send:
Optional 'thank-you' note:
Seems like I've got something to look forward to!
Hope you make it soon!
Stuart

Ranch Hand
Posts: 3141
• Number of slices to send:
Optional 'thank-you' note:
Hi Joel,
The point of the exercise is to high-light the problem you're seeing and understand why it happens
Except your number should be higher 99.9999999999999999 vs
0.9999999999999999. Are you adding 0.1 a thousand times?
Jane

Joel Cochran
Ranch Hand
Posts: 301
• Number of slices to send:
Optional 'thank-you' note:
Arrrggghhhh.... I was afraid that would be the answer...
I'm only doing 10 until figure this out. I originally wrote it for 1000 and then whittled it down to here so I could work on this bug.
Thanks... don't I even get a hint where to look?
------------------

Joel Cochran

Ranch Hand
Posts: 1070
• Number of slices to send:
Optional 'thank-you' note:
I don't think there is anything to look up. What you are seeing is correct. The assignment doesn't ask you to make it equal or make it not equal, it just asks you to print out what it is and be able to explain why.
Remember with floats and doubles, you lose precision.
Bill

Joel Cochran
Ranch Hand
Posts: 301
• Number of slices to send:
Optional 'thank-you' note:
Well, I guess I'm half way there then... I can print it out no problem, just can't explain why it is thus... Back to the books...
------------------

Joel Cochran

Ranch Hand
Posts: 351
• Number of slices to send:
Optional 'thank-you' note:
Joel,
I took a very boring CS course several years ago titled "Numerical Methods". The class dealt with algorithms that are used to minimize error in computer calculations. This was a huge problem in the good old days and can still be if you aren�t careful with your type casting and equations.
You'll be able to solve this with a little research. I know I don't remember the specifics, but I've got time to read before I get to Java-7.
Mike

Sheriff
Posts: 9109
12
• Number of slices to send:
Optional 'thank-you' note:
If you have Just Java 2 (the textbook), look at pages 129 and 130.

Joel Cochran
Ranch Hand
Posts: 301
• Number of slices to send:
Optional 'thank-you' note:
Unfortunately, I do not. I have Sam'sTeach Yourself Java 1.1 in 21 Days, Java 2 Weekend Crash Course, Java in a Nutshell, Wrox (Horton's) Beginning Java 2, Wrox Proffessional Java Server Programming, J2EE Edition, and Sun's Core Servlets and JavaServer Pages. I was kind of hoping I wouldn't have to buy another book...
------------------
Joel Cochran
[This message has been edited by Joel Cochran (edited April 03, 2001).]

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?
reply
Similar Threads