Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Java 7 - Sum

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like I've got something to look forward to!
Hope you make it soon!
Stuart
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have Just Java 2 (the textbook), look at pages 129 and 130.
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic