why does std::numeric_limits<float>::min() differ in behavior when streamed to output with different functions?

时光怂恿深爱的人放手 提交于 2019-12-11 02:32:15

问题


I got a weird behaviour with

 std::numeric_limits<float>::min()

when I call std::cout I get an output value of 1.17549e-38

in contrast when I use

printf("%f", std::numeric_limits<float>::min());

I get a value of 0.000000.

note that when I evaluate (std::numeric_limits<float>::min() == std::numeric_limits<float>::min()) I get true (which is intuitive and logical)

so, can any one explain to me this difference in output?


回答1:


cppreference for numeric_limits:

std::numeric_limits<float>::min() returns FLT_MIN.

The returned value (which is not 0 in reality) displayed using %f is actually formatted to fixed number of decimal places. The '%f' format prints 6 decimal places in fixed format.

You can use :

%e=gives the scientific notation
%g=handles large floating numbers


来源:https://stackoverflow.com/questions/41319177/why-does-stdnumeric-limitsfloatmin-differ-in-behavior-when-streamed-to-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!