Re: Casting double to int produces inconsistent results from VS 2003 to VS 2008

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx> wrote in message news:OWCOGQBsJHA.3648@xxxxxxxxxxxxxxxxxxxxxxx
KD wrote:
I am porting some legacy code from VS 2003 to VS 2008, and I've
encountered a problem. Here is an example to illustrate:

{
double f = 10000000000.0;

int d = (int) (f);
}

When I run from VS2003, I get:
d = 1410065408

When I run from VS2008, I get:
d = -2147483648

Can someone please explain why d2 is different in VS 2008?

I suspect that the difference appeared in VS2005 when there was major work done on the back end for floating point code generation (both for corectness and performance). It's not surprising that an undefined case like this is apparently handled differently. You might experiment with the /fp options, particularly /fp:fast, which IIRC most closely approximates the model used in previous versions of the compiler.

-cd

If you compile with warning level 4, do you get warnings under both compilers?

Anthony Wieser
Wieser Software Ltd

.



Relevant Pages

  • Re: Using ternary and summing array
    ... too, would be initialized to all zeros, due to its global scope. ... Note that my compiler creates awith zero members. ... int main ... The above gives no errors or warnings, even at max warning level, and prints "0". ...
    (comp.lang.c)
  • Re: Unused arguments
    ... On Jul 25, 3:33 am, Christopher Benson-Manica> Old Wolf ... One can either turn down the warning level ... hushes the compiler when one knows what one is doing. ... int foo ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)