aspose file tools
The moose likes Beginning Java and the fly likes Occasional inaccuracy in addition calculations. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Occasional inaccuracy in addition calculations. Why?" Watch "Occasional inaccuracy in addition calculations. Why?" New topic
Author

Occasional inaccuracy in addition calculations. Why?

Richard Berry
Greenhorn

Joined: Jan 18, 2011
Posts: 2
I am not a total beginner at programming, but I am a total newbie with Java. I followed this tutorial http://netbeans.org/kb/docs/java/gui-functionality.html#Exercise_1, and had no problem doing the exercise, but the results are confusing me.

The attached screenshot shows an error of .000002 when adding 12.6 and 9.3.

Some results compute perfectly though. 11.25 + 13.5 gives 24.75, but 111.3 + 50.9 gives 162.20001

The relevant code is:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// First we define float variables.
float num1, num2, result;
// We have to parse the text to a type float.
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
// Now we can perform the addition.
result = num1+num2;
// We will now pass the value of result to jTextField3.
// At the same time, we are going to
// change the value of result from a float to a string.
jTextField3.setText(String.valueOf(result));
}

I'm happy to post all my source code if required, though I have not deviated from the exercise except that my buttons 1 and 2 are swapped around, but I cannot see how that would matter.

I would be very appreciative of any assistance.



[Thumbnail for Addition anomally.jpg]

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

Because computers are dumb.

Most decimal numbers cannot be accurately represented by a computer - at least when using the IEEE standard on how to do it. Basically, they just get 'close enough'. 0.5 and 0.25 can be, since they represent powers of 2 (2^-1 and 2^-2 respectively), but everything else pretty much has some round off error.


Never ascribe to malice that which can be adequately explained by stupidity.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

A computer can only store floating point numbers as a summation of a * 2^i, with a being either 0 or 1 and i being any integer value. For byte, short, int and long i is limited to non-negative numbers. For short and double it's also possible to use negative number, leading to 1/2, 1/4, 1/8 etc.

Because of this, a computer has as much trouble writing 1/10 (0.1) fully as we have writing 1/3 fully. 1/10 is 1/16 + 1/32 + 1/256 + 1/512 + ...
If we translate this to binary, using 0.1 being 1/2, 0.01 being 1/4 etc then 1/10 is 0.000110011001100110011... At some point it has to truncate, just like we truncate 1/3 to 0.3333333333... This truncation causes the errors, just like we wouldn't get 1 if we would multiply 0.3333333333 by 3.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Richard Berry
Greenhorn

Joined: Jan 18, 2011
Posts: 2
OK, thanks. That answers the why.

Your answers helped me to know what to look for in a web search. Here is a good explanation with some suggestions of how to get around the problem:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32704
    
    4
Welcome to the Ranch

I am a bit surprised nobody directed you to our very own FAQ. Look at no 20.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That's because I don't know that FAQ by heart and was too lazy too look it up. Strangely, I wasn't too lazy too type in my full answer. Guess I'm a complex person.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

I think the page that Richard posted a link to explains it quite nicely, so I added that link to our FAQ page. Thanks, Richard!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Occasional inaccuracy in addition calculations. Why?
 
Similar Threads
what this signature means?
using database connection in jframes
Float to String
Reading RS232 and displaying contents on Frame
Erroneous sym type What does this mean?