Testing (in)equality against a const float

Discussion in 'C++' started by Michael Klatt, May 21, 2004.

  1. I've been looking through the FAQ and Googling previous threads on
    c.l.c++ but I haven't seen this exact situation addressed. I
    initialize variables of type float to a known invalid value (of type
    const float), and at a later time I want to see if these variables are
    still undefined:

    #include <iostream>
    int main()
    {
    const float undefined(-999); // valid only as an initial value

    float f1(undefined);
    if (f1 == undefined)
    {
    std::cout << "f1 has not been defined\n";
    }

    float f2(undefined);
    f2 = 5;
    if (f2 != undefined)
    {
    std::cout << "f2 has been defined\n";
    }
    return 0;
    }

    Expected output:

    f1 has not been defined
    f2 has been defined

    From what I've read, I think (hope) that floating point comparisons
    are okay in this case because I'm using the same type for each
    variable in the comparisons and no arithmetic is involved.
     
    Michael Klatt, May 21, 2004
    #1
    1. Advertising

  2. Michael Klatt

    Mike Wahler Guest

    "Michael Klatt" <> wrote in message
    news:...
    > I've been looking through the FAQ and Googling previous threads on
    > c.l.c++ but I haven't seen this exact situation addressed. I
    > initialize variables of type float to a known invalid value (of type
    > const float), and at a later time I want to see if these variables are
    > still undefined:
    >
    > #include <iostream>
    > int main()
    > {
    > const float undefined(-999); // valid only as an initial value
    >
    > float f1(undefined);
    > if (f1 == undefined)
    > {
    > std::cout << "f1 has not been defined\n";
    > }
    >
    > float f2(undefined);
    > f2 = 5;
    > if (f2 != undefined)
    > {
    > std::cout << "f2 has been defined\n";
    > }
    > return 0;
    > }
    >
    > Expected output:
    >
    > f1 has not been defined
    > f2 has been defined
    >
    > From what I've read, I think (hope) that floating point comparisons
    > are okay in this case because I'm using the same type for each
    > variable


    The issue isn't the types, but the values.

    > in the comparisons and no arithmetic is involved.


    Some value other than 'undefined' might evaluate 'close enough'
    for == to return true. But I think this would only be the case
    when 'undefined' is not an integral value. If I'm wrong about this,
    I'm sure I'll be corrected.

    -Mike
     
    Mike Wahler, May 21, 2004
    #2
    1. Advertising

  3. Michael Klatt

    Pete Becker Guest

    Mike Wahler wrote:
    >
    > Some value other than 'undefined' might evaluate 'close enough'
    > for == to return true. But I think this would only be the case
    > when 'undefined' is not an integral value. If I'm wrong about this,
    > I'm sure I'll be corrected.
    >


    Integral values with absolute value < 2^bits, where 'bits' is the number
    of bits in the mantissa, can be represented exactly in binary floating
    point types.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, May 22, 2004
    #3
    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. bd
    Replies:
    0
    Views:
    673
  2. Replies:
    4
    Views:
    286
    Mark P
    Nov 7, 2006
  3. Javier
    Replies:
    2
    Views:
    628
    James Kanze
    Sep 4, 2007
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,660
    James Kanze
    Oct 8, 2009
  5. Anton81

    Float precision and float equality

    Anton81, Dec 5, 2009, in forum: Python
    Replies:
    26
    Views:
    1,169
Loading...

Share This Page