summaryrefslogtreecommitdiff
path: root/pod
diff options
context:
space:
mode:
authorJohn P. Linderman <jpl@research.att.com>2003-07-10 06:45:06 -0400
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2003-07-11 19:37:34 +0000
commit0319020171ad67607dd7a06e238d6c91ff7d4639 (patch)
tree19402c153de30b39d89708b176ec6e6daf1dfab0 /pod
parentb49f3be6643e5cc849effc933e4e4e100d534a4e (diff)
downloadperl-0319020171ad67607dd7a06e238d6c91ff7d4639.tar.gz
Re: Sort sub and NaNs
From: "John P. Linderman" <jpl@research.att.com> Message-Id: <200307101445.KAA79110@raptor.research.att.com> Warn against a potential edge case with sort, <=> and NaNs. p4raw-id: //depot/perl@20156
Diffstat (limited to 'pod')
-rw-r--r--pod/perlfunc.pod9
1 files changed, 9 insertions, 0 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 3686736fcf..612b9cdd70 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -4758,6 +4758,15 @@ inconsistent results (sometimes saying C<$x[1]> is less than C<$x[2]> and
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