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 :-)
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));
}
EDIT: isFinite() is probably the wrong name for this function, but I don't have a better one :/
EDIT2: This answer is bad in the sense that it probably doesn't cover all possible forms of NaN that can be encountered in Monkey C (the IEEE 754 spec reserves ~16 million bit patterns for NaN).
See Travis.ConnectIQ's comment for the correct answer:
[https://forums.garmin.com/developer/connect-iq/f/discussion/338071/testing-for-nan/1777041#1777041]
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.