LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2006, 02:30 PM   #1
qwerty
Member
 
Registered: Feb 2005
Location: England
Distribution: Ubuntu 5.10
Posts: 80

Rep: Reputation: 15
Java double addition gives incorrect answers


Hi, part of an application I'm writing needs to increment a double by 0.0001, however the application seems to give the wrong result

for example I'm expecting

Code:
double currentVal = -0.0003;
		double increment = 0.0001;
		for( int i = 0; i < 6; i++)
		{
			System.out.println(currentVal);
			currentVal = currentVal + increment;
		}
to return...

Code:
-3.0E-04
-2.0E-04
-1.0E-04
0.0
1.0E-04
2.0E-04
however it prints out..

Code:
-3.0E-4
-1.9999999999999998E-4
-9.999999999999998E-5
2.710505431213761E-20
1.0000000000000003E-4
2.0000000000000004E-4
Please can anyone help?

TIA
 
Old 02-18-2006, 02:52 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Without going into the standard of how floats and doubles are stored; don't expect a floating point to be correct. If you need to check a value of a floating point give it a rounding range which it can be in.

If you look at the results from the expected you will see theres not much difference:
expected = -2.0E-04
result = -1.9999999999999998E-4
diff = 2.0E-20
 
Old 02-18-2006, 05:18 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Dmail is absolutely correct - and it's one of those "fundamentals" that anybody who writes a computer program should really be aware of.

"Floating point" vs "Integer" is the fundamental distinction, but the theory behind floating point is quite large.

Interestingly, "newer" is not necessarily "better". FORTRAN did a lot better job than C, and C does a better job than Java (as far as I know, Java and C# are pretty much "the same thing" as far as floating point).

Here are a few links that might be of interest:

http://www.cs.princeton.edu/introcs/91float/

http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf

http://www.ibiblio.org/pub/languages/fortran/ch1-2.html

http://developers.sun.com/prodtech/c...fp_errors.html

... and, my favorite ...

http://www.cs.berkeley.edu/~wkahan/ARITH_17.pdf
 
Old 02-18-2006, 05:20 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by dmail
Nobody is correct 100% of the time.
but doubles are correct 99.999999999999% of the time
 
Old 02-18-2006, 05:49 PM   #5
qwerty
Member
 
Registered: Feb 2005
Location: England
Distribution: Ubuntu 5.10
Posts: 80

Original Poster
Rep: Reputation: 15
Thanks for you help, I've managed to get the results I want by using something along the lines of

Code:
double currentVal = -0.0003;
double increment = 0.0001;
		
DecimalFormat Currency = new DecimalFormat("#0.0000");   
		
currentVal += increment;
currentVal = Double.valueOf(Currency.format(currentVal));
 
Old 02-19-2006, 03:20 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by graemef
but doubles are correct 99.999999999999% of the time
better than humans then
 
Old 02-19-2006, 03:41 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The key thing to be aware of is that floating point operations are generally neither "correct" nor "wrong" - they're *approximations*.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pending or incorrect answers in Linux + Test King for Xk0-002 Kydd Linux - Certification 7 07-14-2009 05:06 AM
Incorrect double click on occasion when using a USB mouse arobinson74 Linux - Hardware 1 02-02-2006 11:55 PM
Java | C | C++ - Float/Double question Mega Man X Programming 6 01-01-2004 10:14 AM
executable jar in java (cannot double click) spyghost Programming 3 09-29-2003 09:13 AM
java covert double into string iceman47 Programming 10 05-28-2003 10:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration