For floating point numbers, you have 32 bits divided into three sections.
A sign bit ( positive or negative )
8 bit exponent ( 1 to 126 are negative exponents and 128 to 254 are positive )
23 mantissa bits ( integral numbers ) a range from 0 to 8,388,607
Code: Select all
struct FloatingPoint
{
FloatingPoint( const float val )
{
int ival = *reinterpret_cast< const int* >( &val );
signbit = ( ival & expMask ) >> 31;
exponentbits = ( ival & expMask ) >> 23;
precisionbits = ival & manMask;
}
operator float()const
{
int ival = ( signbit << 31 ) | ( exponentbits << 23 ) | ( precisionbits & 0x007FFFFF );
return *( reinterpret_cast< float* >( &ival ) );
}
static constexpr auto sgnMask = 0x80000000u;
static constexpr auto expMask = 0x7F800000u;
static constexpr auto manMask = 0x007FFFFFu;
unsigned int signbit : 1;
unsigned int exponentbits : 8;
unsigned int precisionbits : 23;
};
#include <iostream>
int main()
{
constexpr size_t szfp = sizeof( FloatingPoint );
auto fp = FloatingPoint( 0.f );
float fval = 0.f;
for( int counter = 0; counter < 1'000'000; ++counter )
{
std::cout << "Sign bit: " << fp.signbit << "\n";
std::cout << "Exponent: " << fp.exponentbits << "\n";
std::cout << "Mantissa: " << fp.precisionbits << "\n";
fval += .000001f;
fp = FloatingPoint( fval );
}
return 0;
}
Ran the loop from 0 to 1,000,000 incrementing the float value by .000001f each step. This should have ended on 0.999999, but the end result was 1.00903893f. This is the accumulation error you get by adding floats together over time. It's usually best to multiply, but even then you can still get rounding errors.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com