I just found a bug in some number manipulations in my program and I'm getting a FloatDomainError (NaN)
我刚刚在我的程序中发现了一些数字操作的错误,我得到一个FloatDomainError(NaN)
So I started logging the number passed in with:
所以我开始记录传入的号码:
if(metric.is_a?(Numeric))
self.metric = metric
else
LOGGER.warn("metric #{metric} is not a number")
self.metric=0
end
But the number being passed in is NaN
which apparently is_a?(Numeric)
as I don't get my log warning, and it passes metric on to my metric= method, which is where I get my FloatDomainError
但传入的数字是NaN,显然是__?(数字)因为我没有得到我的日志警告,它将度量传递给我的metric =方法,这是我得到我的FloatDomainError的地方
Now, correct me if I'm wrong, but doesn't it seem semantically wrong to have an NaN
(Not A Number) be of type Numeric ?? Can someone explain this to me?
现在,如果我错了,请纠正我,但是如果NaN(非数字)是Numeric类型,那么在语义上是否错误?谁可以给我解释一下这个?
BTW using Jruby-1.4.1
BTW使用Jruby-1.4.1
2 个解决方案
#1
5
IEEE 754 floating point numbers define -INFINITY +INFINITY and NotANumber to make it possible to react to lets say division by zero. you can also calculate with these for eg 2 + INF = INF
IEEE 754浮点数定义-INFINITY + INFINITY和NotANumber以使得可以做出反应,假设除以零。你也可以用这些来计算例如2 + INF = INF
NaN isn't a uniqe ruby feature, they are numeric in java, c++, ... too
NaN不是一个独特的ruby功能,它们在java,c ++等中也是数字
#2
7
I think that making NaN a number makes perfectly sense...
我认为让NaN成为一个数字非常有意义......
try 0.0 / 0.0 in irb -> the result is NaN (which in this case is infinity)
在irb中尝试0.0 / 0.0 - >结果是NaN(在这种情况下是无穷大)
Infinity is mathematically kind of a number, but still, you can't express it with a datatype... in math you use a special symbol too...
Infinity在数学上是一个数字,但是,你仍然不能用数据类型来表达它......在数学中你也使用了一个特殊的符号......
PS: You can use metric.nan? to check it... then it should work as you expect...
PS:你可以使用metric.nan?检查它...然后它应该按预期工作...
#1
5
IEEE 754 floating point numbers define -INFINITY +INFINITY and NotANumber to make it possible to react to lets say division by zero. you can also calculate with these for eg 2 + INF = INF
IEEE 754浮点数定义-INFINITY + INFINITY和NotANumber以使得可以做出反应,假设除以零。你也可以用这些来计算例如2 + INF = INF
NaN isn't a uniqe ruby feature, they are numeric in java, c++, ... too
NaN不是一个独特的ruby功能,它们在java,c ++等中也是数字
#2
7
I think that making NaN a number makes perfectly sense...
我认为让NaN成为一个数字非常有意义......
try 0.0 / 0.0 in irb -> the result is NaN (which in this case is infinity)
在irb中尝试0.0 / 0.0 - >结果是NaN(在这种情况下是无穷大)
Infinity is mathematically kind of a number, but still, you can't express it with a datatype... in math you use a special symbol too...
Infinity在数学上是一个数字,但是,你仍然不能用数据类型来表达它......在数学中你也使用了一个特殊的符号......
PS: You can use metric.nan? to check it... then it should work as you expect...
PS:你可以使用metric.nan?检查它...然后它应该按预期工作...