[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
g++ bug with double function result in <= expression?
From: |
Jonathan Knispel |
Subject: |
g++ bug with double function result in <= expression? |
Date: |
Fri, 01 Oct 2004 22:36:37 +0800 |
User-agent: |
KNode/0.7.7 |
This looks a bit like a compiler bug to me, but I may be missing something
obvious. :-)
The code's below. "a" and "b" are a pair of structs with identical
values, but "a.toReal() <= b.toReal()" returns true if toReal() is defined
with a float return value (expected) and false if defined with a double
return value (huh?). If I assign the results of the calls to a pair of
appropriately typed variables and test the variables with <=, the problem
goes away (workaround).
Here's the sample output from a couple of runs:
Using double
a < b false
a <= b false
ad < bd false
ad <= bd true
Using float
a < b false
a <= b true
ad < bd false
ad <= bd true
I've tried with g++ 3.2.2 (i386-redhat-linux), 3.3.3 (i586-suse-linux) and
3.4.2 (i686-pc-linux-gnu). Stepping through in gdb, the values all look
fine, but the <= test for the function calls fails. Any ideas?
Thanks in advance,
Jonathan
#include <sys/time.h>
#include <iostream>
using std::cout;
#if 1
typedef double RealType;
const char* realTypeName = "double";
#else
typedef float RealType;
const char* realTypeName = "float";
#endif
struct TimeVal : public timeval
{
TimeVal(RealType t) {
tv_sec = static_cast<unsigned int>(t);
tv_usec = static_cast<unsigned int>(t - (t - tv_sec));
}
RealType toReal() { return tv_sec + 0.0000001 * tv_usec; }
};
int
main(int argc,
char** argv)
{
cout << "Using " << realTypeName << '\n';
TimeVal a(2.1);
TimeVal b(2.1);
if (a.toReal() < b.toReal())
cout << "a < b true\n";
else
cout << "a < b false\n";
if (a.toReal() <= b.toReal())
cout << "a <= b true\n";
else
cout << "a <= b false\n";
RealType ad = a.toReal();
RealType bd = b.toReal();
if (ad < bd)
cout << "ad < bd true\n";
else
cout << "ad < bd false\n";
if (ad <= bd)
cout << "ad <= bd true\n";
else
cout << "ad <= bd false\n";
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- g++ bug with double function result in <= expression?,
Jonathan Knispel <=