• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sum lack of precision!

 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While working ahead instead of twiddling my thumbs, doing other work I'm supposed to be doing, and waiting for my latest submission to be nitpicked, I had a go at assignment 7. I get the expected answer up to 2 iterations of the loop, then all hell breaks loose! At least precision-wise. What's up with that? Where should I look for enlightenment oh wise ones?
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had a go at Sum to see what you were talking about. Yikes! Talk about not being in Kansas anymore.
I suppose it has something to do with how rounding works with floating-point numbers, or ultimately with the IEEE Standard for floating-point arithmetic. I'm looking at the Java Language Specification as well as at some descriptions of the IEEE Standard, but as I'm no math geek, I could easily get lost in there. I await with you for pearls of wisdom from the anointed.
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol the whole thing about this assignment is to show you that Float "looses" precision. ie that care should be taken in using it.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not specific to Java. Try a basic Computer Science book (I don't have one to recommend) for an explanation of the way computers store and use floating point numbers.
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marilyn!
I feel sheepish saying this, but I don't have any basic computer science textbooks: I'm coming to programming from philosophy, where they don't teach floating-point arithmetic. It's this sort of thing that continually reminds me of all the bizarre gaps in my knowledge.
I'll ferret around for some basic text.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1821
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anybody (like me) who's made or is making the transition to a computer-related profession from a liberal arts background, I'd highly recommend the book CODE: The Hidden Language of Computer Hardware and Software by Charles Petzold.
It's hard to explain what this book is about -- it ties together all sorts of seemingly disparate topics related to how computers work, with a focus on computing as a communicative activity. It's great fun to read. Kind of gives a sense of the things I (hopefully) would have learned about in a traditional computer science program.
You won't find extensive discussions of data structures or algorithms, like you would in an introductory computer science text, but there are a few pages on floating-point numbers.
[This message has been edited by Michael Matola (edited May 28, 2001).]
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is there any way to fix it, or is this one of those piles of elephant poop in the middle of the room that everybody notices but just doesn't talk about?
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love your elephant poop analogy
Must say I always do talk about the elephant poop when I see it, but I'm normally in the Kruger Park when I do talk about it
I bet there is a very good explanation why it goes wrong and I bet there are even some very clever people that actually understand why, I'm not one of them.
The only thing that I do know is, I try and avoid float as far as I can.
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam, don't worry...you aren't the only one here coming from Philosophy
Carol, there is a class we used in Grains ( don't want to give anything away... ) that I think has a Sister class to handle Decimals correctly. At least I hope so since my company applications rely heavily on ACCURATE decimals!
------------------
I'm a soldier in the NetScape Wars...
Joel
 
Joe McGuire
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't sell a philosophy background short! I got my undergraduate degree in philosophy as well, and I can tell you that all those symbolic logic classes provided an excellent background for programming!
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,
Technically there isn't a way to "fix" floating point numbers, but there are algorithms and methods that have been developed to minimize error when using floating point numbers.
I took a class a long time ago called Numerical Methods (CS414). I can say my career has not hinged on the information dispersed in this incredibly boring class.
The textbook is still in publication if you want to delve into it.
Elementary Numerical Analysis: An Algorithmic Approach

You can pick up a used copy for just $16 at amazon!
Michael
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info- I'm wondering if we're supposed to make this work or just humbly accept the results we get and consider ourselves warned?
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,
Do a search of the Cattle Drive and I think you'll find old comments referring to the intent of the Sum exercise.
Post from 4/6/2001
I'm at the same stage of the Cattle Drive that you are at, but my impression from previous posts is that we are suppose to do the exercise and than explain the results in our e-mail when submitting Java-7.
Good Luck,
Michael
[ edited to update link -ds ]
[ November 02, 2002: Message edited by: Dirk Schreckmann ]
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carol Murphy:
Thanks for the info- I'm wondering if we're supposed to make this work or just humbly accept the results we get and consider ourselves warned?

Yep
 
Brad Ford
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious - what's the code for making the green super smiley face emoticon? Is that just useful in this bboard or can it be used with outlook express etc.?
 
Paul Ralph
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you hit reply, there will be a link to the left of the box that you type in called Smilies Legend. Click on it and you will learn about all the Smilies. But specifically, type this:
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter van der Linden, author of Just Java 2, maintains a Java FAQ list at Java Software, FAQs, Books. The fourth question, under Java Programming Language Issues of the FAQ, explains floating point errors plus it has two additional links that give in-depth explanation of floating point.
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee I visited his FAQ yesterday but missed the floating point. Will go look again.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found this direct link to an article recommended by Just Java 2's author Van der Linden:
What Every Computer Scientist Should Know About Floating Point Arithmetic
I haven't actually read it, it looks a little scary...

[This message has been edited by Pauline McNamara (edited June 11, 2001).]
 
Paul Ralph
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch. Light reading, it ain't. I tried skimming it and promptly made my headache worse. I sure hope Marilyn doesn't want that technical of an answer for Sum.
BTW, I was a Mathematics/Actuarial Science major. Serious flashbacks.
Paul R
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yikes! Printed that one out and I'm sure it won't take long for those pages to hit the recycling pile (unread).
Followed the link you gave Richard - now that's more like it.

[This message has been edited by Pauline McNamara (edited June 12, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic