How to test for a NaN
-
I am trying to test if a number is
NaN
after some derivative computation...I can throw an assertion in the console, but I'd like to be able to detect the
NaN
in code so I can force it, let's say to0.0
instead.When I simply print the number, I get "nan" in the console.
But I can't seem to detect it in script:Console.print(myNumber); // nan if (myNumber == "nan") // fails to detect myNumber = 0.0; if (myNumber == NaN) // also fails to detect myNumber = 0.0;
Though if I do:
// force NaN myNumber = NaN; if (myNumber == NaN) // This "forced" NaN is detected myNumber = 0.0;
So why this difference between two NaNs? Is one with cheese while the other is raw?
-
@ustk I'm not in front of my computer and I might be talking nonsense but, since you're looking for 'NotANumber', what if you test it like this? :
if(yourNumber! = typeof(number))
-
-
@d-healey ah, good to know!
-
@d-healey @Matt_SF thanks both, but I should have mentioned that when the value is computed and throws a
nan
in the console, then the variabletypeof
is still evaluated asnumber
which is probably a bug.The
nan
from computed value is different than aNaN
manually forced. I'll put a snippet together tomorrow to demonstrate the issue...@Christoph-Hart, anything about this?
-
So here's a little snippet
HiseSnippet 987.3ocsV01aaaCDlxIpXR6ErBre.2xWlMZpi8VW2vJRWS7KCdqwwntqneqflhphyxjFjT1yXI+G1O08OX6njhsbcPZqAlLfgt6dtiO2QdG0HshwMFkl3E7xUy4DuOye7JoMoSBUHIC5R79B+KnFKWCEpNe0bpwviHddG7KNEdAGRxe9me9bZJUx3aTQHuRIX7mKlIrazN5Y+lHMsOMh+RwrJnezyFvTxNpTUFxmC7aQlSYSoukOj5fUym3cudQBqRO1RsbCh4bUzpwIpkxB7uRXDSR4Ng1jwXfJTS5jHRiFcStZHDuCGsIyOnHy+J+KDQh052TA9xbCvFOpVC7pcWTp8GAk7pPoCKnz88Gyzh41MVb74S8GHwMjXJVpqRkBrjZ+8A9cTHBos4L5TdeMJr1i5OtUqiA7uFOIDQYTo7lrTNUWGUDFdxIfMQqVZ.JHoRPDCyoZ5L3TnUnPlJjbHNSxrBkDh3ZwBpUrfWm1H7uBCzbalVBsgGBWPsIMYJSBZBNoPzHj4hO.ZeB8IgWiqWHKgyl1WoGRGVuR7Z0HmNu6JVE8BZZ9hhTz8Nb5o.pFt5pff..yCShJKMBlvwLhCKoqBCBrXYUECaC2AlQkei0gMCKl.RTVgaNHNm.gAjYylv0GCB6F3n8lMatSjyjQ7Xj4QMbA+OxLVvpJX+wvjLKXviz.EqxSTK3GudovUACR9BVrZ.UtJm5XhFby90bsPZqezSgtbKunvL5rwi608HrnEbcX.O0v2E+WWAe+yF77R7Ag6FXGC9I3HbmpRd8.TwUt2xJs41Av.T7CyyesLOwJ+RLknVWoZoRO0.KS3RHVoYB4awiVXtFFjpXXTmsxk34aF4wxscVpKWYicSjf9W9hN85BO7oUx6qCWeJQIGpr7Kk0yOefVf20Tb7sZy01nUoob8sZ1MtReWNV+liH4Eo0.w94sGRbuOrgDrht3J.UxARg8x47R49pzHWyu68cGoPJGCfu86C5RsT2TlRcHt4bsU3niWW9BbLcwLm.+tbyTqZNNndmAR3nPUTVJ0t87Q2EAkFv5wVCkbCdjFgcU0KJ9HFZ15NGZ9gRw66ORXYI2NGqcKbDqT+evwxqZ9b+dwwXm3FBdne+Wuu2q7dV9WnxrXWGNAVK9Shm+vrYiwaXYbb0kRbTApyql6zRgbKmrqBLlKixE9W7ozXamrWow12XjLixzp2vJ5BbWl8I4ZPNIyu6N.+HBTFZSx6LpVmmg2s9FFa6Psiie69532suN9n80wueec7w6qi+v953O99cz8oOmkYUyJZaHjKF0KeThmWOIEOAleZk7e.Tdk9W
Also, as strange as it sounds, a
NaN
is of typenumber
so checking for this always returns true. There should be a way to just check forval == NaN
instead -
HiseSnippet 999.3ocsVstiaTCE1SxNHlgKhJwCvg7GRT2lMAJEDUao6lKn.cyF0Tp5+pb73zwjI1Q1dRHhceQ3oj2.33YlMYRSJfhnSjhlyU+cN93OOizJF2XTZhWvKVufS79X+wqk13NwTgjLnKw6S8uhZrbMjq5x0KnFCOh34U8GcJ7BNgj87m+vkzDpjw2phPdoRv3OSLWX2pczS+YQRReZD+Eh4k79gOc.SI6nRToHdp52hrfxlQeC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SRwIvroUxeS8x1Nb
if (val == "nan")
Seems to work :)
-
Why not avoid the nan at the source?
inline function derivative(a) { return a == 0 ? -1 : 1 - Math.cosh(a) / Math.sinh(a) + 1/a; }
-