473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding doubles causes unexpected result - bug?

I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?
Thank you
Jul 12 '07 #1
5 2035
Hi!
Does anyone have any ideas why this may be happening?
Thank you
mmm I guess there is a problem with your cpu ;) In my VS I get the correct
result

greets
Daniel
Jul 12 '07 #2
Thanks Dan but it appears this has happened before according to this blog
posting:
http://scrappydog.com/blogs/blog/arc...8/04/9996.aspx

Still no work on why it actually happens.

Justin

"Daniel Marohn" wrote:
Hi!
Does anyone have any ideas why this may be happening?
Thank you

mmm I guess there is a problem with your cpu ;) In my VS I get the correct
result

greets
Daniel
Jul 12 '07 #3
"justinsaraceno" <ju************@discussions.microsoft.comwrote in message
news:EB**********************************@microsof t.com...
I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?
Thank you

This issue comes up over and over. Apparently they don't teach classes in
computer hardware any more in the universities. This is just a simple
consequence of how floating-point numbers are stored in binary machines.
Most floating-point numbers are stored as approximations of what might
otherwise be precise rational values. For instance, 7/5 is exactly 1.4 in
decimal notation, but cannot be represented precisely in conventional
hardware floating-point architectures because the .4 part cannot be
expressed as a sum of the powers of two, a requirement on binary machines.
(This specific example is paraphrased from the first citation below)

There's nothing we can do about it, except adopt techniques in our code to
avoid the problems that arise. For single-precision, don't expect more than
six or maybe seven places of precision, and for double-precision don't
expect more than about fourteen or fifteen decimal places of precision.
Anything that's displayed after that is just noise; throw it away. Don't use
the "=" sign to compare arbitrary floating-point numbers for equality -
instead do comparisons using some fuzz, such as Single.Epsilon. Or handle it
through rounding. Or use Decimal type if it works for you. And so on.

Try googling for terms like "floating-point precision", "floating-point
representation", "IEEE floating-point", "floating-point roundoff", and so
forth, and maybe you'll find an explanation of your own.

Here's a starting point:
http://www.math.grin.edu/~stone/cour...EEE-reals.html

or try Wikipedia on the topic:
http://en.wikipedia.org/wiki/IEEE_754

which also leads to a bunch of good "see also" articles. Of course there
have been and are other representations besides IEEE 754, but this is
nowadays by far the most common.

Think of this as an adventure in learning.

Tom Dacon
Dacon Software Consulting
Jul 12 '07 #4
justinsaraceno <ju************@discussions.microsoft.comwrote:
I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?
Yup - it's due to how floating point arithmetic works.

The two numbers you're *actually* asking the computer to sum are

63881.97000000000116415321826934814453125
and
34297.9800000000032014213502407073974609375

Those are the closest available double values to your original values.

The closest double to the exact result of that addition is:
98179.9500000000116415321826934814453125

Hence the result you're seeing.

See http://pobox.com/~skeet/csharp/floatingpoint.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 12 '07 #5
justinsaraceno <ju************@discussions.microsoft.comwrote:
Thanks Dan but it appears this has happened before according to this blog
posting:
http://scrappydog.com/blogs/blog/arc...8/04/9996.aspx

Still no work on why it actually happens.
The final post on that blog entry is correct - it's inherent in binary
floating point arithmetic. It's not a bug.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
32
by: yukatan | last post by:
Maybe it's a newbie question, but if I have two strings, let's say s1="4" and s2="5", how can I get a new string of value "9", that is, add the two numbers. If I type: var newStr = s1 + s2 all I...
11
by: Michael Lang | last post by:
You'd think that the advantage to extra decimal places in a data type is better accuracy. Well think again. Look at this method and test results: ...
4
by: Matt Billock | last post by:
Hello everyone, I am having some issues with what should be an insanely simple problem. Basically, I'm working on an implementation of the TSP algorithm (I have a static set of 5 locations, so...
5
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
10
by: David Ricker | last post by:
I am having problems adding two numbers. I am trying to add 1.005 and 1.007 to come up with 2.012. Should be easy enough right? Problem is that I keep getting 2.0119999999999996 as my result. ...
3
by: van6 | last post by:
code frag: int main() { float fval1 = 1.0f; float fval2 = 2.0f; double dsum =0.0; // here is the point ; // Are fval1 and fval2 both promoted to type of double prior to adding
13
by: bintom | last post by:
I ran the following simple code in C++ and got unexpected results: float f = 139.4; cout << f; Output: 139.399994;
5
by: Joe | last post by:
I'm trying to add the current time + a randomly generated time w/ millisecond precision, but the numbers aren't making sense. If I add rand_time + now_time on my calculator, I get a different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.