Question

I guess this needs little extra information. But, why is it that way?

(This assumes the 23 bit float type is base on IEEE 754 32 bit binary floating point, and Little Endianness.)

In 32bit ints it's pretty straightforward, since:

int.MaxValue = 0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2147483647

In floats:

0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2.14748365E+9

I get the E+9 notation. But why is it that:

float.MaxValue = 0xFFFF7F7F = 11111111 11111111 01111111 01111111 = 3.40282347E+38

Was it helpful?

Solution

You said:

int.MaxValue = 0x7F, 0xFF, 0xFF, 0xFF = 01111111 11111111 11111111 11111111 = 2147483647

int.MaxValue is indeed 2147483647 because this is a 32-bit value, signed and encoded in two’s complement, which is valued this way in your case:

value = -231b31 + b30•230 + b29•229 + … + b0•20 = 2147483647 (as all bits b0 to b30 are 1 and b31 is 0).

You said:

In floats: 0x7F, 0xFF, 0xFF, 0xFF = 01111111 11111111 11111111 11111111 = 2.14748365E+9

That’s slightly incorrect. What you've done is convert int.MaxValue to float (you’ve not interpreted the encoding of the max int value as float — you’ve changed representations) which is:

2.14748365E+9 ≈ 2147483647 = 2147483647.0 — all the same stuff for humans, but floating-point values and integers are encoded in memory differently

but

2147483647.0’s hex representation (when rounded to a float) is 0x4f000000 not 0x7F, 0xFF, 0xFF, 0xFF.

Here is why (single precision floating point format):

enter image description here

0x4f000000 is valued as (-1)0•(1+0)•2158-127 = 1•1•231 = 231 = 2147483648.0

You may check yourself here online IEE754 converter.

You also said:

float.MaxValue = 0xFF 0xFF 0x7F 0x7F = 11111111 11111111 01111111 01111111 = 3.40282347E+38

The 3.40282347E+38 value is correct, but its hex representation is not 0xFF 0xFF 0x7F 0x7F but 0x7f7fffff.

You may decode 0x7f7fffff this way:

(-1)0•(1+2-1+2-2+2-3+…+2-23)•2254-127 = 1•(1+1)•2127, which is approximately 2•2127 = 2128 ≈ 3.40282347E+38.

You may wonder why the exponent is 254 and not 255. Exponent value 255 is a special case, and values with the exponent set to 255 are treated as +infinity or -infinity (depending on sign bit) if the significand (fraction) field is zero and as NaNs if the significand field is not zero.

OTHER TIPS

It looks like your endianness is confused. The maximum single precision (32-bit) IEEE-754 float value is composed of:

  • one sign bit, which is zero for positive numbers
  • eight bits of exponent, which cannot be all ones (since that would mean the value was NaN), so the maximum value is 11111110 = 254 (biased) = 127 (actual exponent)
  • 23 bits of mantissa, of which the maximum value would be all ones

So I would expect the maximum single-precision float value to look like 0x7F7FFFFF.

Floating point data formats are quite different from integer formats. The value is made up of three components, the sign, the exponent and the significand. And these individual components are spread across multiple bytes within the data format. For example, the sign is a single bit, and is stored in the same byte as another of the components.

The bottom line is that what you know about integer representation is not applicable to floating point representation.

This assumes the 32 bit float type is base on IEEE 754 32 bit binary floating point.

The largest finite float has zero in both the sign bit and the least significant exponent bit. In big-endian hex that is 0x7f7fffff. The sign bit, the most significant bit of the number, is zero to make it positive. The least significant exponent bit, the most significant bit of the second byte, is zero to get a finite number. All exponent bits one is a NaN or infinity.

0xffff7f7f is the little-endian representation.

For IEEE 754-1985 spec, float point is in format of S8.24.
So the Largest normalized number is 2^127*(2-2^-23).
When encoded in binary, results in 0,1111 1110,111 1111 1111 1111 1111 1111.
Please find more detailed in the IEEE 754-1985.

Floats and floating point numbers in general are a little different than ints. Whereas ints is just a two's compliment representation of an integer value, a floating point number consists of different parts: a sign bit (S), the exponent field (E), and the significand or mantissa (M), from left to right

The maximum usable exponent is 0xFE, because 0xFF is a special value indicating an infinity.

So we have a sign bit that is 0 for positive numbers, a maximal exponent of 0xFE, and all 1's for the mantissa, resulting in

SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM

01111111 01111111 11111111 11111111 or 7F 7F FF FF

And since in a little endian machine, the order of the bytes is reversed, what you see is

FF FF 7F 7F

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top