Lawrence Kirby wrote:
This is a reasonable approach, and it isn't obvious what else you could
do without examining the representation which won't be portable. Because
we're in a situation where compilers are not following the standard there
is no competely portable method to do this. However making y volatile will
result in the correct behaviour on many compilers.
Thanks for the reply, Lawrence.
Didn't know about "volatile" - so I've just tried it out, but it doesn't
seem to be the answer in this case.
My solution (so far) is to write a separate function for MS compilers:
#ifdef _MSC_VER // identifies Microsoft compilers
void test_it(double x) {
float foo = x;
float y = foo;
if(x == y) {
printf("x can be represented precisely by a float\n");
}
else {
printf("x can't be represented precisely by a float\n");
}
}
#else // use the original
But that worked only if optimizations were not turned on - in which case
the only solution I could find was to turn 'em off:
#ifdef _MSC_VER
#pragma optimize("", off)
void test_it(double x) {
float foo = x;
float y = foo;
if(x == y) {
printf("x can be represented precisely by a float\n");
}
else {
printf("x can't be represented precisely by a float\n");
}
}
#pragma optimize("", on)
#else
But even that wouldn't work if -O1 optimization (minimize space) were
used - in which case an additional 'foo +=0;' was needed. So, a solution
which I *think* covers all cases with MS compilers is to have:
#ifdef _MSC_VER
#pragma optimize("", off)
void test_it(double x) {
float foo = x;
float y = foo;
foo += 0;
if(x == y) {
printf("x can be represented precisely by a float\n");
}
else {
printf("x can't be represented precisely by a float\n");
}
}
#pragma optimize("", on)
#else
I don't think I've added much overhead to the original (though I haven't
tested) but is there something I could try that is not quite so comical ?
Getting back to "volatile", I found that simply making y volatile did
not achieve the desired result - so I did:
volatile float foo = x;
volatile float y = foo;
if(x == y) { ...
But that still didn't do the right thing, so I added 'foo += 0;' as
before, but that produced something that Microsoft calls an "INTERNAL
COMPILER ERROR".
At that point I gave up on that approach :-)
Cheers,
Rob
--
To reply by email u have to take out the u in kalinaubears.