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.
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…
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…
LOL, now try this:
If I manually set it to NaN, it works with your code, but when it's returned through a Math function, nope! WTH!
rad.equals(NaN)
works for me.
e.g.
var rad = NaN;
System.println("rad == NaN: " + (rad == NaN)); // false
System.println("rad.equals(NaN): " + rad.equals(NaN)); // true
Seems to be an exception to the Monkey C rule that you should be able to use "==" with "primitive" types.
LOL, now try this:
If I manually set it to NaN, it works with your code, but when it's returned through a Math function, nope! WTH!
lmao that's terrible
I guess it's time to report a bug...
Looks like Math.acos(45) actually returns -NaN.
System.println("Math.acos(45): " + Math.acos(45)); // "Math.acos(45): -nan(ind)"
System.println(Math.acos(45).equals(-NaN)); // "false" (not that I expect this to work since NaN * x => NaN, therefore -NaN => NaN)
However:
System.println(Math.acos(45).equals(Math.acos(45))); // "true"
This suggests you can test for -NaN by comparing any number to the result of Math.acos(45) lol. This also suggests an implementation for a function that seems to be sorely needed:
public function isFinite(x as Number or Long or Float or Double) as Boolean { return !x.equals(NaN) && !x.equals(Math.acos(45));
}
That would mean I would need a 'finite' function for all the Math functions I should test against NaN :-O
Edit: Oh, I see what you mean, I'll try it,
Yep, it worked! Thanks for your help :-)
Hi, to increase the accuracy of my results, I've converted all floats to doubles but by doing so, it broke the isFinite function :-( I tried a few variations but non returned false when NaN.double was returned. What would be the magic equation for doubles? Thanks,
BTW, Had to convert from float to double because of funky precision in plain arithmetic.
What's the function and input(s) that return NaN.double (and/or -Nan.double)? You must have at least one example, since you're seeing Nan.double return from somewhere.
Store the result of that function call in a variable and use it with your isFinite() function. It's the same concept behind using Math.acos(45) to get -NaN (float).
You may also have to handle Infinity/-Infinity, although I'm not sure how to derive those values in Monkey C since dividing by zero results in an exception IIRC.