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

#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 

Currently I started working with the Chipmunk physics engine and found this in the header file

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

+7
c ++
source share
4 answers

It sets INFINITY to a float value represented by the hexadecimal bits 0x7f800000, which is +INF . For some reason, Visual Studio does not define INFINITY.

+15
source share

The above code effectively defines a floating point constant with some specific representation of bytes.

Each float is represented by a set of bytes, but when you define the float constants, you are forced to use the decimal representation and cannot determine the constant with the value say 0xFFFFFFFF (I do not know if this constant is a legal float number).

The above code circumvents this limitation - it first sets up a byte array inside the union, and then “accesses” the same byte array, as if it were a float number. This, by the way, is illegal - only a previously established union member can be legally available, but he can work on this particular implementation.

+6
source share

Creates an INFINITY_HACK variable of type MSVC_EVIL_FLOAT_HACK. it sets the Bytes array for the values ​​of the corresponding hexadecimal values. It then converts these bytes into a floating point number (the union allows you to use one of the base values ​​internally, therefore, referring to .value, it converts the data that INIFITY_HACK points to float), following the IEEE-754 Floating-Point Standard (note that bytes are taken in reverse order) for binary-> floating conversion.

There's a good calculator that can explain this if you don't know how it works: http://babbage.cs.qc.cuny.edu/IEEE-754/32bit.html (if you enter 7F800000, you will get Infinity, but try entering 4048F5C2 (you get closer to 3.14) This calculator has a decimal value → hex.

+2
source share

Its a way to initialize the memory occupied by the float variable to 0x7f800000. As @Jim says, this is + infinity in the float.

The code is roughly equivalent:

 byte Bytes[4] = { 0x00, 0x00, 0x80, 0x7F }; float Value; memcpy(&Value, Bytes, 4); #define INFINITY_HACK (Value) 

First, the source code defines a union that allows you to manipulate four bytes of memory, either as an array of four bytes, or as a single float (which, we assume, also takes four bytes):

 union MSVC_EVIL_FLOAT_HACK { unsigned __int8 Bytes[4]; float Value; }; 

He then selects the union instance named INFINITY_HACK and sets the values ​​of its Bytes array to the specified hexadecimal values ​​.:

 static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}}; 

This causes the field to be float initialized, since it also takes up the same memory as the byte array.

Finally, it defines a constant for the processor called INFINITY as a float value.

+1
source share

All Articles