任意浮点值如何与无穷大进行比较? [英] How do arbitrary floating point values compare to infinity?
问题描述
我正在使用 float
类型,我认为可以安全地假定我的目标硬件实现了一个无穷大值。
$ p
$ b $ auto $ leastValue = std :: numeric_limits< float> :: infinity();这是一个示例代码:
(auto i = something.begin(),e = something.end(); i!= e; ++ i)
{
auto value =(* i) - > ;的GetValue();
if(value {
leastValue = value;
$ / code $ / pre
解决方案 IEEE 754浮点数,除了NaN和无穷大外,一切都小于无穷大。有机会,你会在你的平台上有这些。如果你偏执狂,请检查 numeric_limits< float> :: is_iec559
。如果您的平台碰巧不符合,请使用 numeric_limits< float> :: max()
;如果你的序列是非空的,它不会给你一个错误的结果。
Basically i want to search for the smallest (positive) value from a bunch of values and need a default value to compare to the first one. The naïve assumption would be, they always compare "less than" (except NaNs, but let's not consider those) but I'm not quite sure.
I'm using the float
type and I think it can safely be assumed that my target hardware implements a infinity value.
Here's some example code:
auto leastValue = std::numeric_limits<float>::infinity();
for (auto i = something.begin(), e = something.end(); i != e; ++i)
{
auto value = (*i)->GetValue();
if (value < leastValue)
{
leastValue = value;
}
}
解决方案 For IEEE 754 floats, except NaN and infinity, everything is less than infinity. Chances are you'll have those on your platform. If you're paranoid, check with numeric_limits<float>::is_iec559
. If your platform happens to be not conforming, use numeric_limits<float>::max()
; if your sequence is nonempty, it will not give you a wrong result.
这篇关于任意浮点值如何与无穷大进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!