简体   繁体   中英

Obtain minimum NEGATIVE float value in C++

I was looking at std::numeric_limits<float>::min/max() but it appears 'min()' returns the smallest absolute value, not the lowest value. Is it safe to use

-std::numeric_limits<float>::max() , ie is float symmetric in min/max limits?

IEEE 754浮点数使用符号位进行签名(而不是二进制补码),所以如果您确定您的编译器/平台使用该表示(非常常见),那么您可以使用-std::numeric_limits<float>::max()你怀疑-std::numeric_limits<float>::max()

use std::numeric_limits::lowest()

static _Ty __CRTDECL lowest() _THROW0()
    {   // return most negative value
    return (-(max)());
    }

Yes, float is symmetric in minimum/maximum values.

If you're using the lowest representable value as an initial value in searching a list for its maximum value, consider using infinity instead.

std::numeric_limits<T>::has_infinity() will return true for any numeric type that has it and std::numeric_limits<T>::infinity() will return a value that always evaluates greater than any other non-NaN value for that type. This value can be negated and will evaluate less than anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM