assert failure

Discussion in 'C++' started by ken.carlino@gmail.com, Feb 14, 2006.

  1. Guest

    Hi,

    I have the follwing code, but I can't understand why the assert fail?
    I print out the value that I am check, it has the right value, but the
    assert still fail.

    Here is the code:
    // bd.areaPerCent is of type 'float'

    cout << a.areaPerCent << endl;
    assert (a.areaPerCent == 0.00137389);

    Here is the output of the program:
    0.00137389
    snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
    Assertion `a.areaPerCent == 0.00137389' failed.

    why it fails?
     
    , Feb 14, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I have the follwing code, but I can't understand why the assert fail?
    > I print out the value that I am check, it has the right value, but the
    > assert still fail.
    >
    > Here is the code:
    > // bd.areaPerCent is of type 'float'
    >
    > cout << a.areaPerCent << endl;
    > assert (a.areaPerCent == 0.00137389);
    >
    > Here is the output of the program:
    > 0.00137389
    > snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
    > Assertion `a.areaPerCent == 0.00137389' failed.
    >
    > why it fails?
    >


    The == comparison is performed in binary floating point, not the ASCII
    representation that you output. Translations between ASCII and binary
    float are often inexact. It is a basic programming error to use == to
    compare floats.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Feb 14, 2006
    #2
    1. Advertising

  3. Guest

    Thanks. Can you please tell me how should I compare float instead?
     
    , Feb 14, 2006
    #3
  4. Kai-Uwe Bux Guest

    wrote:

    > Hi,
    >
    > I have the follwing code, but I can't understand why the assert fail?
    > I print out the value that I am check, it has the right value, but the
    > assert still fail.
    >
    > Here is the code:
    > // bd.areaPerCent is of type 'float'
    >
    > cout << a.areaPerCent << endl;
    > assert (a.areaPerCent == 0.00137389);
    >
    > Here is the output of the program:
    > 0.00137389
    > snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
    > Assertion `a.areaPerCent == 0.00137389' failed.
    >
    > why it fails?


    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.17

    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 14, 2006
    #4
  5. Ben Pope Guest

    wrote:
    > Thanks. Can you please tell me how should I compare float instead?


    <in Father Jacks voice>

    feq!

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 14, 2006
    #5
  6. Howard Guest

    [OT] Re: assert failure

    "Ben Pope" <> wrote in message
    news:43f19faf$0$8266$...
    > wrote:
    >> Thanks. Can you please tell me how should I compare float instead?

    >
    > <in Father Jacks voice>
    >
    > feq!


    What's "feq" ??? (And who's Father Jack?)

    -Howard
     
    Howard, Feb 14, 2006
    #6
  7. Ben Pope Guest

    Re: [OT] Re: assert failure

    Howard wrote:
    > "Ben Pope" <> wrote in message
    > news:43f19faf$0$8266$...
    >> wrote:
    >>> Thanks. Can you please tell me how should I compare float instead?

    >> <in Father Jacks voice>
    >>
    >> feq!

    >
    > What's "feq" ??? (And who's Father Jack?)


    Hmm, I thought feq was for floating point equals. It's not, my
    subconscious made that up.

    Father Jack is one of the characters from Father Ted, a UK Sitcom, he
    shouts "Feck", "Arse", "Drink" and "Girls", whilst drunk (which is all
    the time).

    Anyway, apologies for the tangent, it's all based on a fictitious
    function called feq.

    Apologies, I'll create one to redeem myself!

    #include <iostream>
    #include <cmath>
    #include <limits>

    template<class T>
    bool feq(T val1, T val2) {
    return (fabs(val1 - val2) <= std::numeric_limits<T>::epsilon());
    }

    int main() {
    float float1 = 1.2;
    float float2 = 1.200000001;

    double double1 = 1.2;
    double double2 = 1.200000001;

    std::cout << (feq(float1, float2) ? "equal\n" : "not equal\n");
    std::cout << (feq(double1, double2) ? "equal\n" : "not equal\n");

    return 0;
    }

    /me runs for cover

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 14, 2006
    #7
  8. Pete Becker Guest

    wrote:
    > Thanks. Can you please tell me how should I compare float instead?
    >


    It depends on what you want to do. In some circumstances, == is just the
    right thing. In others it's not. What are you trying to do?

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Feb 14, 2006
    #8
  9. Guest

    Is the STL equal_to<float> safe?
    Will it have the same problem?

    Thank you.

    Kai-Uwe Bux wrote:
    > wrote:
    >
    > > Hi,
    > >
    > > I have the follwing code, but I can't understand why the assert fail?
    > > I print out the value that I am check, it has the right value, but the
    > > assert still fail.
    > >
    > > Here is the code:
    > > // bd.areaPerCent is of type 'float'
    > >
    > > cout << a.areaPerCent << endl;
    > > assert (a.areaPerCent == 0.00137389);
    > >
    > > Here is the output of the program:
    > > 0.00137389
    > > snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
    > > Assertion `a.areaPerCent == 0.00137389' failed.
    > >
    > > why it fails?

    >
    > http://www.parashift.com/c -faq-lite/newbie.html#faq-29.17
    >
    > Best
    >
    > Kai-Uwe Bux
     
    , Feb 14, 2006
    #9
  10. wrote:

    > Is the STL equal_to<float> safe?


    Yes.

    > Will it have the same problem?


    Yes. In fact, it is defined in terms of ==.

    Did you look at the FAQ that Kai-Uwe Bux referred you to?

    Best regards,

    Tom
     
    Thomas Tutone, Feb 14, 2006
    #10
  11. Re: [OT] Re: assert failure

    In message <43f206ce$0$8224$>, Ben Pope
    <> writes
    >Howard wrote:
    >> "Ben Pope" <> wrote in message
    >>news:43f19faf$0$8266$...
    >>> wrote:
    >>>> Thanks. Can you please tell me how should I compare float instead?
    >>> <in Father Jacks voice>
    >>>
    >>> feq!

    >> What's "feq" ??? (And who's Father Jack?)

    >
    >Hmm, I thought feq was for floating point equals. It's not, my
    >subconscious made that up.
    >
    >Father Jack is one of the characters from Father Ted, a UK Sitcom,


    Irish. Didn't you notice the difference?

    >he shouts "Feck", "Arse", "Drink" and "Girls", whilst drunk (which is
    >all the time).


    --
    Richard Herring
     
    Richard Herring, Feb 15, 2006
    #11
  12. Ben Pope Guest

    Re: [OT] Re: assert failure

    Richard Herring wrote:
    > In message <43f206ce$0$8224$>, Ben Pope
    > <> writes
    >>
    >> Father Jack is one of the characters from Father Ted, a UK Sitcom,

    >
    > Irish. Didn't you notice the difference?


    OK, it was filmed in County Clare, for Channel 4, a UK TV company.

    Apologies to anybody from Southern Ireland (or anywhere else) offended
    by my calling Father Ted a "UK sitcom".

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 15, 2006
    #12
    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. Skywise
    Replies:
    2
    Views:
    323
    David Hilsee
    Aug 22, 2004
  2. Robert Brewer
    Replies:
    1
    Views:
    491
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,526
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    918
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,671
    Richard Bos
    May 17, 2010
Loading...

Share This Page