Subj : Re: JSVAL_IS_NUMBER(NaN) == TRUE (!?) To : Rob Swindell From : Brendan Eich Date : Fri Feb 18 2005 02:55 pm Rob Swindell wrote: > SpiderMonkey oddity: > > I found it odd that if jsval is NaN, a test of JSVAL_IS_NUMBER(jsval) would > evaluate as TRUE. Sure, it's of "number" type (see also typeof(0/0)). > And my native methods don't consider NaN a valid number either, so I needed a > valid test for is number, but non NaN, so I came up with: > > #include /* JSDOUBLE_IS_NaN() */ > > JSBool > jsval_isNaN(JSContext *cx, jsval v) > { > jsdouble d; > > if(JSVAL_IS_DOUBLE(v)) { > if (!JS_ValueToNumber(cx, v, &d)) No need to convert v to number if it's already a double (which is a subtype of number). > return JS_FALSE; > if(JSDOUBLE_IS_NaN(d)) > return JS_TRUE; > } > return JS_FALSE; > } > > // Replacement for JSVAL_IS_NUMBER(): > #define JSVAL_IS_NUM(cx,v) (JSVAL_IS_NUMBER(v) && !jsval_isNaN(cx,v)) > > I'm surprised there's not something like this already in the jsapi. Yeah, it hasn't come up till now. Most native methods should let NaNs flow through and produce NaN results. Why not yours? /be .