C ++ providing float size 4 bytes

I need a cross-architectural way of ensuring that the float is 4 bytes (as in 32-bit windows). For example, in the structures that I create, I use __int32instead intto provide an integer value of 4 bytes in length.

How can I do this using float? I know that I can simply substitute a value with a type __int32; however, when casting on a float on 64-bit systems, I will not have problems?

+5
source share
2 answers

I need a cross-architectural way to ensure that the float is 4 bytes

There is no analogue int32_tfor floating point values.

- , , - .

#include <cassert>
int main () {
    assert(sizeof(float) == 4);
    // If control reaches this line, then you've 
    // ensured that float is 4 bytes.


    // rest of your program goes here
}
+7

, . IEEE-754 ( , , ) 4 .

, , , C , . , CPU , , .

, static_assert sizeof(float) == 4; , , , . , ( .)

+3

All Articles