为什么numeric_limits< T> :: min()不返回最小值? [英] Why doesn't numeric_limits<T>::min() return the smallest value?

查看:82
本文介绍了为什么numeric_limits< T> :: min()不返回最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时:

#include <limits>
#include <cstdio>

#define T double

int main()
{
    static const T val = std::numeric_limits<T>::min();
    printf( "%g/2 = %g\n", val, val/2 );
}

我希望看到无法预测的结果.但是我得到正确的答案:

I would expect to see an unpredictable result. But I get the correct answer:

(16:53) > clang++ test_division.cpp -o test_division
(16:54) > ./test_division 
2.22507e-308/2 = 1.11254e-308

这怎么可能?

推荐答案

因为 min 为您提供了最小的 normalized 值.您仍然可以使用较小的 denormalized 值(请参见 http://en.wikipedia.org/wiki/Denormalized_number ).

Because min gives you the smallest normalized value. You can still have smaller denormalized values (see http://en.wikipedia.org/wiki/Denormalized_number).

这篇关于为什么numeric_limits&lt; T&gt; :: min()不返回最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆