Web lists-archives.org

Re: [Mingw-users] typecast runtime




On 2007-12-28 19:56Z, alpha-quadrant@xxxxxxxxxxxxx wrote:
> I know for a fact that typecast (int) without floor() first, gets 
> runtime errors if compiled with VC6.

Are you casting a floating-point value whose integral part
is outside the range representable by int? Consider:

#include <float.h>
#include <math.h>
#include <stdio.h>

int main()
{
    double d;
    int i0, i1;

    d = DBL_MAX;
    i0 = (int)d;
    i1 = (int)floor(d);
    printf("Before decrement:\n  i0 = %d\n  i1 = %d\n", i0, i1);

    --d;
    i0 = (int)d;
    i1 = (int)floor(d);
    printf("After  decrement:\n  i0 = %d\n  i1 = %d\n", i0, i1);

    return i0 == i1;
}

That program's behavior is undefined if DBL_MAX exceeds INT_MAX.
Using MinGW gcc, I get different output with '-O3' vs. '-O0',
and that's okay, because it's undefined behavior. It's also okay
for an implementation to produce a binary that prints an error
message when you run it, or even sends email to the implementor.

> Is this just a VC6 thing or is it also wise to use (int)floor(dblval) 
> with mingw also?

What benefit do you expect to gain by calling floor()? I guess
you're saying it prevents msvc from printing a message when it
detects an error, but inhibiting the message doesn't remove the
error.

What are you actually trying to accomplish with this typecast?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users