473,471 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparing two doubles

I was very surprised by the output of the following program:

#include <iostream>
#include <numeric>

int main() {

double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 };
double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 };
double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
double sum2 = std::accumulate( vals_2, vals_2+5, 0. );
if( sum1 != 1. )
std::cout << "sum1 not ok ";
else
std::cout << "sum1 ok ";
if( sum2 != 1. )
std::cout << "sum2 not ok";
else
std::cout << "sum2 ok";

std::cout << std::endl;
}
the output is: sum1 not ok sum2 ok

Is that behavior to be expected? Do I have to use a function that
compares up to some precision everytime
I want to compare 2 doubles? I tried changing double to float: in that
case both sums are ok.

Jul 19 '05 #1
3 7860
nicolas wrote:
I was very surprised by the output of the following program:

#include <iostream>
#include <numeric>

int main() {

double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 };
double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 };
double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
double sum2 = std::accumulate( vals_2, vals_2+5, 0. );
if( sum1 != 1. )
std::cout << "sum1 not ok ";
else
std::cout << "sum1 ok ";
if( sum2 != 1. )
std::cout << "sum2 not ok";
else
std::cout << "sum2 ok";

std::cout << std::endl;
}
the output is: sum1 not ok sum2 ok

Is that behavior to be expected? Do I have to use a function that
compares up to some precision everytime
I want to compare 2 doubles? I tried changing double to float: in that
case both sums are ok.


See:

http://www.eskimo.com/~scs/C-faq/q14.5.html

(It's from the C-FAQ, but is the same for C++.)

HTH,
--ag

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2
I think the prolem lies with the fact that .1 and .2 cannot be expressed in
binary...
"nicolas" <ni*******@yahoo.com> wrote in message
news:bk**********@news.Stanford.EDU...
I was very surprised by the output of the following program:

#include <iostream>
#include <numeric>

int main() {

double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 };
double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 };
double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
double sum2 = std::accumulate( vals_2, vals_2+5, 0. );
if( sum1 != 1. )
std::cout << "sum1 not ok ";
else
std::cout << "sum1 ok ";
if( sum2 != 1. )
std::cout << "sum2 not ok";
else
std::cout << "sum2 ok";

std::cout << std::endl;
}
the output is: sum1 not ok sum2 ok

Is that behavior to be expected? Do I have to use a function that
compares up to some precision everytime
I want to compare 2 doubles? I tried changing double to float: in that
case both sums are ok.

Jul 19 '05 #3
Floating point addition is not commutative, hence your
result. Never count on comparing floating point values
directly (via "=="), instead try comparing their difference
with a threshold value, via "<" and/or ">".

HTH,
- J.
Jul 19 '05 #4

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

Similar topics

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...
6
by: utab | last post by:
Hi there, I would like to compare the values in two vectors of double. I can do it by using iterators, I had an idea but wondering there is a library facility to do that. vector<double> a;...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.