I found the values of Infinity and NaN in my data, but I don’t know how they appeared. Can you tell me about the premise operation that these two values will appear, so I can check it out
With floats, if you divide zero by zero, you get NaN (Not a Number); if you divide anything else by zero, you get positive or negative infinity.
1 Like
Looking through the official documentation, I didn’t find anything about NaN and
Infinity’s judgment, which seems to be a common method in other languages, is missing in motoko.
module {
public func isNaN(v:Float): Bool {
return ((v < 0.000) == false) and ((v > 0.000) == false)
};
public func isInfinite(v:Float): Bool {
return v > 1.8e307
}
}
This is the method I wrote to judge NaN and Infinity, what’s wrong with it. . if so. Please point out where I am wrong
0 is neither <0 or >0. And >1.3e308 tests for +∞ but not -∞ (and floats can in fact go up to 1.79e308).
Sorry, old thread, but here is a correct implementation of these predicates for IEEE floats:
func isNan(x : Float) : Bool { x != x }
func isInfinite(x : Float) : Bool { 1.0/x == 0 }
But of course, we have Float.isNan by now.