Minimum floating point number (closest to zero)

I am trying to find the minimum value (closest to zero) that I can save in a single point with a floating point. Using the header <limits>, I can get the value, but if I make it much smaller, the float can still hold it and it will give the correct 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" retains a good result even after dividing the minimum value by 2 ^ 23, after which it gives 0.

The value that gives numeric_limits<float>::min()is 2 ^ (- 126), which I believe is the correct answer, but why does the float on my program have such small numbers?

+4
3

std::numeric_limits::min , . std::numeric_limits::lowest . IEEE ( ).

+5

wikipedia https://en.wikipedia.org/wiki/Single-precision_floating-point_format:

2 ^ -126 ≈ 1,18 × 10 ^ -38, () 2 ^ -149 ≈ 1,4 × 10 ^ -45.

,

cout  << (float)pow(2,-149) 
      << "-->" << (float)pow(2,-150) 
      << "-->" <<  (float)pow(2,-151) << endl;

:

1.4013e-45-->0-->0
+3

I am trying to find the minimum value (closest to zero) that I can store in a single floating point floating point number

0 is the closest value to 0 that can be stored in any precision float. In fact, you can save it in two ways, since there is a positive and negative 0.

0
source

Source: https://habr.com/ru/post/1623365/


All Articles