Hi, how does one test for NaN? The following if statement is skipped, although 'rad' is NaN
Lang.NaN doesn't work either (doesn't compile). "try/catch" doesn't catch it either.
Thanks.
Hi, how does one test for NaN? The following if statement is skipped, although 'rad' is NaN
Lang.NaN doesn't work either (doesn't compile). "try/catch" doesn't catch it either.
Thanks.
I think this should work.
const FLT_MAX = 3.4028235e38f; function isnan(x as Float) as Boolean { return x != x; } function isinf(x as Float) as Boolean { return (x < -FLT_MAX || FLT_MAX…
That's bizarre but I think I found a workaround for you.
System.println(Math.acos(45d).toFloat().equals(Math.acos(45))); // true
class MathIsFun { static const negativeNan as Float = Math.acos…
Yep, that did the trick, thanks again :-)
Hard to find one when even this returns false :-(
That's bizarre but I think I found a workaround for you.
System.println(Math.acos(45d).toFloat().equals(Math.acos(45))); // true
class MathIsFun { static const negativeNan as Float = Math.acos(45); // (add ".toFloat()" to make type checker happy) public function isFinite(val as Number or Long or Float or Double) as Boolean { var valAsFloat = val.toFloat(); // this assumes that Infinity/-Infinity aren't possible in Monkey C. // if they are, we need to find a way to produce those values // (division by zero produces an error or exception) return !valAsFloat.equals(NaN) && !valAsFloat.equals(negativeNan); } }
EDIT: isFinite is clearly the wrong name for this function and Travis.ConnectIQ's answer below is better.
It also makes sense that NaN != NaN and Math.acos(45d) != Math.acos(45d) [Math.acos(45d) is -NaN], since all forms of NaN are supposed to be not equal to any number, including NaN. Trying to get around that fact using equals() is clearly the wrong approach, especially since there are far more than 2 NaNs in the IEEE 754 floating point spec.
The correct way to implement isnan is definitely Travis.ConnectIQ's approach (x != x).
Yep, that did the trick, thanks again :-)
I think this should work.
const FLT_MAX = 3.4028235e38f; function isnan(x as Float) as Boolean { return x != x; } function isinf(x as Float) as Boolean { return (x < -FLT_MAX || FLT_MAX < x); }
Hi!
It does work in this way too
This fails for someNumber = Math.acos(45), for example.
var n = Math.acos(45);
System.println(n); // "-nan(ind)"
System.println(n.equals(NaN)); // "false"
System.println(n.toString().equals("nan")); // "false"
Travis.ConnectIQ's answer above is the correct approach.