I'm trying to find the minimum value (closest to zero) that I can store in a single precission floating point number. Using the <limits>
header I can get the value, but if I make it much smaller, the float can still hold it and it gives the right result. Here is a test program, compiled with g++ 5.3.0.
#include <limits>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a = numeric_limits<float>::max();
float b = numeric_limits<float>::min();
a = a*2;
b = b/pow(2,23);
cout << a << endl;
cout << b << endl;
}
As I expected, "a" gives infinity, but "b" keeps holding the good result even after dividing the minimum value by 2^23, after that it gives 0.
The value that gives numeric_limits<float>::min()
is 2^(-126) which I belive is the correct answer, but why is the float on my progam holding such small numbers?
If you mean integer then 1 (or -1) is the closest integer to zero. I expect however that you mean rational number or real number. In this case there is no number closest to zero. You can see this by letting be any real or rational number that is not equal to zero.
A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not.
std::numeric_limits::min
for floating-point types gives the smallest non-zero value that can be represented without loss of precision. std::numeric_limits::lowest
gives the smallest representable value. With IEEE representations that's a subnormal value (previously called denormalized).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With