Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Testing (in)equality against a const float

Reply
Thread Tools

Testing (in)equality against a const float

 
 
Michael Klatt
Guest
Posts: n/a
 
      05-21-2004
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.
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      05-21-2004

"Michael Klatt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      05-21-2004
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)
 
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
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting 'float (*)[4]' to 'const float**' mathieu C++ 9 09-15-2007 09:10 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const against non const pointer mathon@gmx.at C++ 4 11-07-2006 09:00 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments