I am learning c++ and I am really trying to get a handle on how numbers are stored and manipulated in memory. To help test my understanding, I wrote the following program:
#include <iostream>
#include <cmath>
int main () {
int answer = 200*300*400*500;
std::cout << "int result: " << answer << std::endl;
std::cout << "Size of int result: " << sizeof(answer) << std::endl;
std::cout << "Is normal? " << isnormal(answer) << std::endl;
float f_answer = 200.0*300*400*500;
std::cout << "float result: " << f_answer << std::endl;
std::cout << "Size of float result: " << sizeof(f_answer) << std::endl;
std::cout << "Is normal? " << isnormal(f_answer) << std::endl;
float under = 0.0000000005 * 0.0000000001 * 0.0000000001 * 0.0000000001 * 0.00001;
std::cout << "under result: " << under << std::endl;
std::cout << "Is normal?: " << isnormal(under) << std::endl;
return 0;
}
Based on everything that I have been learning about IEEE I was expecting the last block to return an underflow value (or 0) because it was my understanding that the most precise that 32bit floats can be is around e^-38.
To my surprise, I was able to get it all the way down to e^-45! This made me start to look into normal vs. subnormal. So, awesome...something, somewhere is figuring out that I want more precision and deciding that this variable should be stored as subnormal. My question is: how?? What is doing this? Is this c++ itself?? Is it the compiler? (I'm using clang, to the best of my knowledge.) I thought the major benefit of using a language like this is that I can be pretty damn sure of what it's doing when I tell it to save as a float. My apologies if this is an ill-formed question, this is truly my first foray into lower level languages and any clarification that folks can provide would be greatly appreciated!