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

why error double + double ???

Code:

double total = 0;
double a1 = 28.69;
double a2 = 14.21;
total = a1 + a2;

I builde it in Vs2008,and get the result as:
total = 42.900000000000006
Attached Images
File Type: jpg Error.jpg (15.6 KB, 652 views)
Jul 5 '11 #1
9 2529
It's because of the precision. In fact, when you do operations with doubles you can't expect to have perfect results.

It is often useful to compare doubles using a particular technique, instead of writing:

Expand|Select|Wrap|Line Numbers
  1. double a, b;
  2. if (a == b)
you use an "eps" constant (very very little) and do the inverse:

Expand|Select|Wrap|Line Numbers
  1. const double eps = 1e-9;
  2. double a, b;
  3. if ( fabs(a-b) < eps)
Jul 5 '11 #2
But when I use the two operators is the decimal it for the right result. The comparison with how you say it will only be used when applied to some cases but if so when you have to compare like to see an addition is correct?
Jul 5 '11 #3
I dont' understand you.

If you have to check the correctness of an addition you can use EPS.

Expand|Select|Wrap|Line Numbers
  1. if (total == a1+a2)
becomes
Expand|Select|Wrap|Line Numbers
  1. if ( fabs(total-(a1+a2)) < EPS)
Jul 5 '11 #4
my mind to ask why there is no exact results when added two double numbers, but exactly when add two decimal numbers.

You have a comprehensive solution for the case added two double numbers this?
Jul 6 '11 #5
code green
1,726 Expert 1GB
The problems you are encountering are part of basic computer science.
That is how a computer stores floating point numbers.
I recommend some background reading on this subject

When comparing floats it is general practice to set a tolerance, which is what Willian explained to you.

You define a constant EPS (epsilon) as the acceptable tolerance say
0.0000001.

Then subtract actual and expected results and compare to EPS as William showed.
if outside the tolerance the result is rejected.
Jul 6 '11 #6
Look here to understand how double works.
Jul 6 '11 #7
If the comparison as you say, when I perform operations that are intermediate calculations over to do it the way you say?
Jul 6 '11 #8
code green
1,726 Expert 1GB
Interesting link William.
I'm afraid I could not understand the OP last question
Jul 7 '11 #9
bvrwoo
16
Read on conversion. No percision is exact with double (and even floats). You have to remember that you are using 8 bytes of memory with 64 bits of 1 and 0's in machine code for your number. It can only get so precise with floating points of exponents (include 32bit (float) exponent of 8).
Aug 29 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mensan | last post by:
I am trying to compute the differnece between two dwords in visual basic and keep getting the wrong value being computed. I have the following structure defind: Public Type HighLowQuote...
6
by: Barry | last post by:
The following code compiled using MSVC++6.0 - double di=0.0; for(;di<=512.0;) { string.Format("%li",int(di)); pDC-> TextOut( left +int(di), 100,string); di += 51.2; }
12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
0
by: Ken Allen | last post by:
Recently I placed a number of files that I am developing with .Net under VSS control. Ever since that time I am getting an odd error when I double-click on one or more of the files: An error...
0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
3
by: Terrence Chan | last post by:
I have try the below and the problem is my xmlstring is something like "<employee name="Joh"n"> strXML = Replace(strXML, "&", "&amp;") strXML = Replace(strXML, "<", "&lt;") strXML = Replace(strXML,...
5
by: SharkHunter | last post by:
Hi all, I am new to PHP programming. When I started with small examples of PHP, i landed with such errors which make me feel annoying. Incase of this snippet --------- 06 <?php 07 echo...
1
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ...
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
2
by: Tyler Palmer | last post by:
I am having a problem with my program. I cant figure out a solution for the compiler error im getting. In function `double endingConversion(double)': invalid operands of types `double...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.