473,394 Members | 1,703 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,394 software developers and data experts.

Adding two doubles gives incorrect result?

Hi

Looking at the following code ...

double a = 100.1;
double b = 0.1;
double c = a + b;

I would like to know why the value of c is 100.19999999999999 and not 100.2?

What should I do to correct this.

Thanks.
Apr 29 '07 #1
3 8963
Floating point types (Float, Double) cannot hold exactly certain decimal
numbers. You could use the Decimal type if you need exact decimal
arithmetic, or use a subroutine package (e.g. GMP Bignum Library) if your
needed precision is greater than what Decimal can provide.

Or, you could do your arithmetic in floating point, convert the answer to
Decimal, and then round that to the precision you need.

"Fabio" <Fa***@discussions.microsoft.comwrote in message
news:2E**********************************@microsof t.com...
Hi

Looking at the following code ...

double a = 100.1;
double b = 0.1;
double c = a + b;

I would like to know why the value of c is 100.19999999999999 and not
100.2?

What should I do to correct this.

Thanks.


Apr 29 '07 #2
It has to do with the way a PC stores floating point types. if one value
compared to the other is too much smaller, the computer "discards" part
of the smaller number leading to "errors" like this.

Apr 29 '07 #3
Fabio <Fa***@discussions.microsoft.comwrote:
Looking at the following code ...

double a = 100.1;
double b = 0.1;
double c = a + b;

I would like to know why the value of c is 100.19999999999999 and not 100.2?
See http://pobox.com/~skeet/csharp/floatingpoint.html
What should I do to correct this.
Well, you're not going to get infinite precision with any of the built-
in types, but for arithmetic like the above, the decimal type would
help you.

--
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
Apr 29 '07 #4

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,...
2
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
13
by: Yogesh Khanolkar | last post by:
Hi, I am getting incorrect o/p from the code below: #include<stdio.h> #include<float.h> #include<limits.h> main() { double val,val1;
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. ...
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
5
by: =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post by:
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.