Connecting Tech Pros Worldwide Help | Site Map

Testing (in)equality against a const float

  #1  
Old July 22nd, 2005, 12:25 PM
Michael Klatt
Guest
 
Posts: n/a
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.
  #2  
Old July 22nd, 2005, 12:25 PM
Mike Wahler
Guest
 
Posts: n/a

re: Testing (in)equality against a const float



"Michael Klatt" <mdklatt@ou.edu> wrote in message
news:2cb75565.0405211120.6b033ade@posting.google.c om...[color=blue]
> 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[/color]

The issue isn't the types, but the values.
[color=blue]
> in the comparisons and no arithmetic is involved.[/color]

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


  #3  
Old July 22nd, 2005, 12:25 PM
Pete Becker
Guest
 
Posts: n/a

re: Testing (in)equality against a const float


Mike Wahler wrote:[color=blue]
>
> 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.
>[/color]

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)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM