Aren't 6 digits guarantee in single precision?

前端 未结 2 704
无人共我
无人共我 2021-01-16 05:56

Here\'s the code:

typedef std::numeric_limits fl;

int main()
{   
    std::cout.precision(100);    

    float f1 = 9999978e3;
    std::cout &l         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-16 06:34

    Not in the way you think it's guaranteed, no.

    By way of offering a counter-example, for an IEEE754 single precision floating point, the closest number to

    9999990000
    

    is

    9999989760
    

    What is guaranteed is that your number and the float, when both are rounded to six significant figures, will be the same. This is be the value of FLT_DIG on your platform, assuming it implements IEEE754. E.g. the closest float number to 9999979000 is 9999978496.

    See http://www.exploringbinary.com/floating-point-converter/

提交回复
热议问题