Comparing two doubles

Discussion in 'C++' started by nicolas, Sep 20, 2003.

  1. nicolas

    nicolas Guest

    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.
     
    nicolas, Sep 20, 2003
    #1
    1. Advertising

  2. nicolas

    Artie Gold Guest

    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
     
    Artie Gold, Sep 20, 2003
    #2
    1. Advertising

  3. nicolas

    JustSomeGuy Guest

    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.
    >
     
    JustSomeGuy, Sep 20, 2003
    #3
  4. 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.
     
    Jacek Dziedzic, Sep 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. {AGUT2} {H}-IWIK
    Replies:
    4
    Views:
    2,865
    Marcelo Pinto
    Sep 12, 2003
  2. dan
    Replies:
    1
    Views:
    2,323
    Jack Klein
    Nov 26, 2003
  3. John Smith

    comparing doubles for equality

    John Smith, Dec 30, 2006, in forum: C Programming
    Replies:
    12
    Views:
    722
  4. Thomas Kowalski

    Comparing doubles

    Thomas Kowalski, Jul 9, 2007, in forum: C++
    Replies:
    28
    Views:
    1,142
    kwikius
    Jul 11, 2007
  5. vk
    Replies:
    23
    Views:
    1,374
    James Kanze
    Jan 14, 2009
Loading...

Share This Page