typedef struct FloatBits FloatBits;
struct FloatBits {
unsigned int significand : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
};
typedef union FloatIntBits FloatIntBits;
union FloatIntBits {
float the_float;
int the_int;
FloatBits the_bits;
};
int main(void) {
FloatIntBits fib = {.the_int = 0};
// Powers of 2
fib.the_bits.significand = 0;
fib.the_bits.exponent = 1;
fib.the_bits.sign = 0;
printf("Fields are B=0x%x, E=%i, 0x%x; float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1.0;
printf("Fields are B=0x%x, E=%i, 0x%x; float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 2.0;
printf("Fields are B=0x%x, E=%i, 0x%x; float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 8.0;
printf("Fields are B=0x%x, E=%i, 0x%x; float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1024.0;
printf("Fields are B=0x%x, E=%i, 0x%x; float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
return 0;
}
```
---
## Output
Fields are B=0x0, E=-126, 1+0x0; float is 0.000000
Fields are B=0x0, E=0, 1+0x0; float is 1.000000
Fields are B=0x0, E=1, 1+0x0; float is 2.000000
Fields are B=0x0, E=3, 1+0x0; float is 8.000000
Fields are B=0x0, E=10, 1+0x0; float is 1024.000000
---
## Now with Significand
```C
#include
#include
typedef struct FloatBits FloatBits;
struct FloatBits {
unsigned int significand : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
};
typedef union FloatIntBits FloatIntBits;
union FloatIntBits {
float the_float;
int the_int;
FloatBits the_bits;
};
int main(void) {
FloatIntBits fib = {.the_int = 0};
// Powers of 2
fib.the_bits.significand = 0;
fib.the_bits.exponent = 1;
fib.the_bits.sign = 0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 2.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 8.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1024.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
// Adding 1
fib.the_float = 3.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 9.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1025.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
return 0;
}
```
---
## Output
Fields are (-1)^0 * 2^-126 * 1+(0/2^23); float is 0.00000000000000000000000000000000000001175494350822
Fields are (-1)^0 * 2^0 * 1+(0/2^23); float is 1.000000
Fields are (-1)^0 * 2^1 * 1+(0/2^23); float is 2.000000
Fields are (-1)^0 * 2^3 * 1+(0/2^23); float is 8.000000
Fields are (-1)^0 * 2^10 * 1+(0/2^23); float is 1024.000000
Fields are (-1)^0 * 2^1 * 1+(4194304/2^23); float is 3.000000
Fields are (-1)^0 * 2^3 * 1+(1048576/2^23); float is 9.000000
Fields are (-1)^0 * 2^10 * 1+(8192/2^23); float is 1025.000000
---
## Integers and Floats
* Integer math and floating point math is different
* Special hardware exists for both
* ALUs (Arithmetic Logic Units) for integers
* FPUs (Floating Point Units) for floats
* These are the only real "types" in your computer architecture
---
## Takeaways
* Floating point math is more complicated
* And we'll also have to worry about rounding
* Next time!
* But floats are elegantly crafted
---
## Equations
* Subnormals
* exponent is 0
* value = $(-1)^{sign} \times 2^{1-bias} \times (\frac{Significand}{2^{Sig~Bits}})$
* Normals
* value = $(-1)^{sign} \times 2^{exponent-bias} \times (1 + \frac{Significand}{2^{Sig~Bits}})$