在C ++中获得最小的NEGATIVE浮点值

时间:2022-06-16 22:52:43

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 :: min / max()但看起来'min()'返回的是最小的绝对值,而不是最低的值。使用安全吗?

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

-std :: numeric_limits :: max(),即在最小/最大限制中是浮点对称的吗?

3 个解决方案

#1


24  

IEEE 754 floating point numbers use a sign bit for signed-ness (rather than something like twos complement), so if you're sure that your compiler/platform uses that representation (very common) then you can use -std::numeric_limits<float>::max() as you suspected.

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

#2


21  

use std::numeric_limits::lowest()

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

#3


4  

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.

对于任何具有它的数值类型,std :: numeric_limits :: has_infinity()将返回true,并且std :: numeric_limits :: infinity()将返回一个总是计算得比任何其他非NaN值大的值对于那种类型。这个值可以被否定,并且评估的价值低于其他任何值。

#1


24  

IEEE 754 floating point numbers use a sign bit for signed-ness (rather than something like twos complement), so if you're sure that your compiler/platform uses that representation (very common) then you can use -std::numeric_limits<float>::max() as you suspected.

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

#2


21  

use std::numeric_limits::lowest()

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

#3


4  

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.

对于任何具有它的数值类型,std :: numeric_limits :: has_infinity()将返回true,并且std :: numeric_limits :: infinity()将返回一个总是计算得比任何其他非NaN值大的值对于那种类型。这个值可以被否定,并且评估的价值低于其他任何值。