aspose file tools*
The moose likes Cattle Drive and the fly likes Sum lack of precision! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Sum lack of precision!" Watch "Sum lack of precision!" New topic
Author

Sum lack of precision!

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1187
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

Joined: Apr 16, 2001
Posts: 76
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

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
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

Joined: Mar 25, 2001
Posts: 1740
    
    2
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

Joined: Mar 15, 2001
Posts: 1187
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

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Mar 23, 2001
Posts: 301
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


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
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

Joined: Mar 11, 2001
Posts: 351
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

Joined: Mar 15, 2001
Posts: 1187
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

Joined: Mar 11, 2001
Posts: 351
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

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Mar 22, 2001
Posts: 40
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

Joined: Aug 10, 2000
Posts: 312
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

Joined: Mar 01, 2001
Posts: 233
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

Joined: Jan 27, 2001
Posts: 5089
Gee I visited his FAQ yesterday but missed the floating point. Will go look again.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Aug 10, 2000
Posts: 312
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sum lack of precision!
 
Similar Threads
Career dilemma
Very Serious Memory Problem
Float precision problem?
About servlet's service() method synchronize
Precision and scale of a decimal value