I believe the key is that every non-relational operation
on Nan produces NaN -- this includes unary minus,
and every relational operation/function, except isnan,
involving NaN is false -- this includes "NaN==NaN" is false
(there are so many new math functions that I probably missed
some exceptions, like the classifier functions)
so
abs(-NaN) => abs(NaN) => NaN
NaN < 0 # false
NaN == 0 # false
NaN > 0 # false
but the basic premise is that NaN is not intuitive w.r.t. elementary arithmetic
and therefore regression tests involving isnan(x) need to be separated out from
those involving !isnan(x)
-- Glenn Fowler -- AT&T Research, Florham Park NJ --
On Thu, 30 Nov 2006 05:40:28 +0100 Roland Mainz wrote:
> While writing some regression tests I found a weired detail.
> The following test script (not exactly correct, but that is not the
> issue in thid case ; note that you need something like ksh93s- or better
> for this)
> -- snip --
> function err_exit
> {
> print -u2 -n "\t"
> print -u2 -r ${Command}[$1]: "${@:2}"
> let Errors+=1
> }
> alias err_exit='err_exit $LINENO'
> float x
> float y
> ### snip ###
> (
> float m
> float c
> for x in 0 1 -1 -Inf +Inf NaN ; do
> for y in 0 1 -1 -Inf +Inf NaN ; do
> if (( y < 0.0 && y != NaN )) ; then
> (( m=-abs(x) ))
> else
> (( m=abs(x) ))
> fi
>
> (( c=copysign( x, y ) ))
> [ $c == $m ] || err_exit "(( copysign( $x, $y ) != $m ; $c ))"
> done
> done
> )
> -- snip --
> results in
> $ ksh test_solaris.sh
> [68]: (( copysign( 1, NaN ) != 1 ; -1 ))
> [68]: (( copysign( -1, NaN ) != 1 ; -1 ))
> [68]: (( copysign( -Inf, NaN ) != Inf ; -Inf ))
> [68]: (( copysign( Inf, NaN ) != Inf ; -Inf ))
> -- snip --
> I've tried a small experiment and changed
> -- snip --
> for y in 0 1 -1 -Inf +Inf NaN ; do
> -- snip --
> to
> -- snip --
> for y in 0 1 -1 -Inf +Inf -NaN ; do
> -- snip --
> ...which causes the test to pass...
> ...however I am slightly confused about a "negative NaN" - sure, the
> floating point datatype allows this since the sign occupies a seperate
> bit - but is |-NaN| something valid (I think the answer should be "yes"
> but I am somewhat confused that the Solaris manual pages have zero
> information about this detail... ;-( ) ?
> How should C99 |printf()| and the shell's "printf" builtin handle this,
> e.g. should the sign be printed or not (quick test: $ ksh -c 'float x ;
> x=-NaN ; printf "%f $x\n" $x' # returns "NaN NaN" and no sign of a
> "sign") ?
> Does the C99 standard say anything about this ?
> ----
> Bye,
> Roland
> --
> __ . . __
> (o.\ \/ /.o) roland.mainz at nrubsig.org
> \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
> /O /==\ O\ TEL +49 641 7950090
> (;O/ \/ \O;)
> _______________________________________________
> ksh93-integration-discuss mailing list
> ksh93-integration-discuss at opensolaris.org
> http://mail.opensolaris.org/mailman/listinfo/ksh93-integration-discuss