Change 20156 by [EMAIL PROTECTED] on 2003/07/11 19:37:34

        Subject: Re: Sort sub and NaNs 
        From: "John P. Linderman" <[EMAIL PROTECTED]>
        Date: Thu, 10 Jul 2003 10:45:06 -0400
        Message-Id: <[EMAIL PROTECTED]>
        
        Warn against a potential edge case with sort, <=> and NaNs.

Affected files ...

... //depot/perl/pod/perlfunc.pod#398 edit

Differences ...

==== //depot/perl/pod/perlfunc.pod#398 (text) ====
Index: perl/pod/perlfunc.pod
--- perl/pod/perlfunc.pod#397~20155~    Fri Jul 11 12:11:17 2003
+++ perl/pod/perlfunc.pod       Fri Jul 11 12:37:34 2003
@@ -4758,6 +4758,15 @@
 sometimes saying the opposite, for example) the results are not
 well-defined.
 
+Because C<< <=> >> returns C<undef> when either operand is C<NaN>
+(not-a-number), and because C<sort> will trigger a fatal error unless the
+result of a comparison is defined, when sorting with a comparison function
+like C<< $a <=> $b >>, be careful about lists that might contain a C<NaN>.
+The following example takes advantage of the fact that C<NaN != NaN> to
+eliminate any C<NaN>s from the input.
+
+    @result = sort { $a <=> $b } grep { $_ == $_ } @input;
+
 =item splice ARRAY,OFFSET,LENGTH,LIST
 
 =item splice ARRAY,OFFSET,LENGTH
End of Patch.

Reply via email to