Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum floating point number (closest to zero)

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?

like image 426
Msegade Avatar asked Jan 08 '16 01:01

Msegade


People also ask

What is the closest number to 0?

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.

Is 0.0 A float number?

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.


1 Answers

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).

like image 54
Pete Becker Avatar answered Oct 09 '22 16:10

Pete Becker