How to workaround the inconsistent definition of numeric_limits::min()?

前端 未结 7 1362
予麋鹿
予麋鹿 2021-02-08 00:05

The numeric_limits traits is supposed to be a general way of obtaining various type infomation, to be able to do things like

template
T min(con         


        
7条回答
  •  囚心锁ツ
    2021-02-08 00:26

    numeric_limits::min returned the lowest negative number, all floating point number types, return the smallest positive number when I tried it with Sun CC & g++.

    I guess this is because 'smallest' and 'minimum' mean different things with floating point numbers. It is a bit odd though.

    Both Sun CC and g++ produce the same result :

    short:min: -32768 max: 32767

    int:min: -2147483648 max: 2147483647

    unsigned int:min: 0 max: 4294967295

    long:min: -2147483648 max: 2147483647

    float:min: 1.17549e-38 max: 3.40282e+38

    double:min: 2.22507e-308 max: 1.79769e+308

    long double:min: 3.3621e-4932 max: 1.18973e+4932

    unsigned short:min: 0 max: 65535

    unsigned int:min: 0 max: 4294967295

    unsigned long:min: 0 max: 429496729

    template
    void showMinMax()
    {
        cout << "min: " << numeric_limits::min() << endl;
        cout << "max: " << numeric_limits::max() << endl;
        cout << endl;
    }
    
    int main()
    {
    cout << "short:";
    showMinMax()
    ...etc...etc..
    

提交回复
热议问题