Can someone explain me NaN in Ruby?

I just found an error in some number manipulation in my program and I get FloatDomainError (NaN)

So, I started logging the number passed 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 that is passed is NaN , which apparently is_a?(Numeric) , since I don't get a warning about my protocol, and it passes the metric to my metric = method, where I get my FloatDomainError

Now, correct me if I'm wrong, but doesn't it seem semantically wrong to have NaN (Not A Number) of type Numeric? Can someone explain this to me?

BTW using Jruby-1.4.1

+6
ruby numbers nan
source share
2 answers

The IEEE 754 floating point numbers specify -INFINITY + INFINITY and NotNumber so that you can respond to allow division by zero. you can also calculate with them e.g. 2 + INF = INF

NaN is not a unique ruby ​​function, they are numeric in java, C ++, ... too

+5
source share

I think creating a NaN number makes sense ...

try 0.0 / 0.0 in irb -> NaN result (which is infinite in this case)

Infinity is mathematically a number, but still you cannot express it using a data type ... in mathematics you also use a special character ...

PS: Can you use metric.nan? check it out ... then it should work as you expect ...

+7
source share

All Articles