# is_nan()

8 messages
Open this post in threaded view
|

## is_nan()

 I'm sure this has come up before, but in a cursory check I didn't see anyone else post an actual solution. The FP value `nan` is handled in OpenSCAD in a very awkward manner. First-off, there's no constants for nan or inf, so lets work around that:         NAN = acos(2);         INF = 1/0; Secondly, the comparators for nan, while arguably true from an academic point of view, are almost completely useless from a programming perspective: - `is_num(NAN)` returns false. (Even though nan is of the same type as inf and 0.) - `NAN == NAN` returns false. - `NAN != NAN` returns true. Combined, these really make it very ugly to tell if a value is nan.  The obvious tests are all useless:         x = foo();         if (x==NAN) echo("This branch is NEVER executed for any value or type of x.");         if (x!=NAN) echo("This branch is executed for ALL values of x, no matter the type."); As far as I can tell, the only way to detect NaN correctly is a complex construct like this:         function is_nan(x) =                 !is_num(x) && !is_undef(x) && !is_string(x) &&                 !is_list(x) && !(x<=INF) && is_undef(x[0]); The !(x<=INF) test disallows INF, -INF, and booleans. The is_undef(x[0]) shows that x is not a range like [0:10]. - Revar _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: is_nan()

 function is_nan(x) = (x != x) ? true : false; On February 7, 2020 at 00:25:59, Revar Desmera ([hidden email]) wrote: I'm sure this has come up before, but in a cursory check I didn't see anyone else post an actual solution. The FP value `nan` is handled in OpenSCAD in a very awkward manner. First-off, there's no constants for nan or inf, so lets work around that: NAN = acos(2); INF = 1/0; Secondly, the comparators for nan, while arguably true from an academic point of view, are almost completely useless from a programming perspective: - `is_num(NAN)` returns false. (Even though nan is of the same type as inf and 0.) - `NAN == NAN` returns false. - `NAN != NAN` returns true. Combined, these really make it very ugly to tell if a value is nan. The obvious tests are all useless: x = foo(); if (x==NAN) echo("This branch is NEVER executed for any value or type of x."); if (x!=NAN) echo("This branch is executed for ALL values of x, no matter the type."); As far as I can tell, the only way to detect NaN correctly is a complex construct like this: function is_nan(x) = !is_num(x) && !is_undef(x) && !is_string(x) && !is_list(x) && !(x<=INF) && is_undef(x[0]); The !(x<=INF) test disallows INF, -INF, and booleans. The is_undef(x[0]) shows that x is not a range like [0:10]. - Revar _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: is_nan()

 Huh.  Even shorter:function is_nan(x) = x!=x;That is somehow both elegant in its simplicity and horribly twisted in its logic.- RevarOn Feb 7, 2020, at 12:31 AM, Whosawhatsis <[hidden email]> wrote:function is_nan(x) = (x != x) ? true : false;On February 7, 2020 at 00:25:59, Revar Desmera ([hidden email]) wrote:I'm sure this has come up before, but in a cursory check I didn't see anyone else post an actual solution. The FP value `nan` is handled in OpenSCAD in a very awkward manner. First-off, there's no constants for nan or inf, so lets work around that: NAN = acos(2); INF = 1/0; Secondly, the comparators for nan, while arguably true from an academic point of view, are almost completely useless from a programming perspective: - `is_num(NAN)` returns false. (Even though nan is of the same type as inf and 0.) - `NAN == NAN` returns false. - `NAN != NAN` returns true. Combined, these really make it very ugly to tell if a value is nan. The obvious tests are all useless:x = foo(); if (x==NAN) echo("This branch is NEVER executed for any value or type of x."); if (x!=NAN) echo("This branch is executed for ALL values of x, no matter the type."); As far as I can tell, the only way to detect NaN correctly is a complex construct like this: function is_nan(x) = !is_num(x) && !is_undef(x) && !is_string(x) && !is_list(x) && !(x<=INF) && is_undef(x[0]); The !(x<=INF) test disallows INF, -INF, and booleans. The is_undef(x[0]) shows that x is not a range like [0:10]. - Revar _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: is_nan()

 On 07.02.20 11:02, Revar Desmera wrote: > function is_nan(x) = x!=x; > That is somehow both elegant in its simplicity and horribly > twisted in its logic. Also nobody will claim any responsibility if it breaks at some point. If it makes sense to have that check, it would probably be better to actually implemented it in the core like the other test functions. The missing tokens are indeed a problem as that is one case where we can export files as CSG that can't be imported back.    nan = 0/0;    inf = 1/0;    ninf = -1/0;    echo(inf, ninf, nan); Just to highlight the point it's not an OpenSCAD invention: \$ python3 Python 3.7.6 (default, Jan 19 2020, 22:34:52) [GCC 9.2.1 20200117] on linux Type "help", "copyright", "credits" or "license" for more information.  >>> import math  >>> nan = math.nan  >>> nan == nan False  >>> nan != nan True  >>> ciao,    Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Torsten
Open this post in threaded view
|

## Re: is_nan()

Open this post in threaded view
|