Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this code do: static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};

Tags:

c++

#ifndef INFINITY
#ifdef _MSC_VER
    union MSVC_EVIL_FLOAT_HACK
    {
        unsigned __int8 Bytes[4];
        float Value;
    };
    static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
    #define INFINITY (INFINITY_HACK.Value)
#endif

I'm currently getting started with the Chipmunk physics engine and found this in a header file

INFINITY is used to set infinite momentum for objects, however I don't understand what this code above does!

like image 458
Ham Avatar asked Nov 04 '10 10:11

Ham


1 Answers

It sets INFINITY to the float value represented by the hex bits 0x7f800000, which is +INF. Visual Studio doesn't define INFINITY for some reason.

like image 80
Jim Buck Avatar answered Oct 04 '22 13:10

Jim Buck