ajith.mk
ajith.mk

Reputation: 85

Floats in visual studio

Consider single precision floating point number system conforming to IEEE 754 standard. In visual studio, FP switch was set to Strict.

struct FP {
unsigned char a : 8;
unsigned char b : 8;
unsigned char c : 8;
unsigned char d : 8;
}*fp;
fp->a = 63;
fp->b = 128;
fp->c = 0;
fp->d = 1;
std::cout << "raw float = " << *reinterpret_cast<float*>(fp) << "\n";

Tha mathematical value according to standard is 1.00000011920928955078125.

What visual studio prints is raw float = 2.36018991e-38. Why?

Assume sign bit it 0. And 0111 1111 in exponent part.

In the remaining 23 bits assume 01 and 10 are the least significant bits, which means the mathematical value is number1 = 1.00000011920928955078125 and number2 = 1.0000002384185791015625 respectively. The mid value is number3 = 1.000000178813934326171875. So, all values between number1 and number3 should be captured by encoding with 01 in least two significant bits and values between number3 and number2 should be captured by encoding with 10 in least significant bits. But visual studio captures 1.0000001788139343(this actually falls between number1 and number3) and greater values in encoding with 10 in least significant bits. So what am I mising?

Upvotes: 0

Views: 1606

Answers (1)

Jenya
Jenya

Reputation: 1145

If you take a look at https://www.h-schmidt.net/FloatConverter/IEEE754.html then you can see that binary representation of 2.36018991E-38 is

00000001 00000000 10000000 00111111 and that binary value equals to your struct

enter image description here

Upvotes: 2

Related Questions