Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Comparing two doubles

Reply
Thread Tools

Comparing two doubles

 
 
nicolas
Guest
Posts: n/a
 
      09-20-2003
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.

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      09-20-2003
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

 
Reply With Quote
 
 
 
 
JustSomeGuy
Guest
Posts: n/a
 
      09-20-2003
I think the prolem lies with the fact that .1 and .2 cannot be expressed in
binary...


"nicolas" <> wrote in message
news:bkgclo$pma$...
> 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.
>



 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      09-21-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accuracy when comparing doubles vk C++ 23 01-14-2009 09:41 AM
Comparing doubles Thomas Kowalski C++ 28 07-11-2007 07:20 PM
comparing doubles for equality John Smith C Programming 12 01-03-2007 01:14 PM
floats doubles long doubles dan C++ 1 11-26-2003 05:12 AM
Comparing two floats or doubles to a precision {AGUT2} {H}-IWIK C++ 4 09-12-2003 02:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57