This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
numeric.c:S_mulexp10 -- quit when you can
[perl5.git] / t / op / numconvert.t
index f71fd6c..fedef70 100755 (executable)
@@ -48,9 +48,22 @@ my $max_chain = $ENV{PERL_TEST_NUMCONVERTS} || 2;
 my $max_uv1 = ~0;
 my $max_uv2 = sprintf "%u", $max_uv1 ** 6; # 6 is an arbitrary number here
 my $big_iv = do {use integer; $max_uv1 * 16}; # 16 is an arbitrary number here
-
-if ($max_uv1 ne $max_uv2 or $big_iv > $max_uv1) {
-  print "1..0\n# Unsigned arithmetic is not sane\n";
+my $max_uv_less3 = $max_uv1 - 3;
+
+print "# max_uv1 = $max_uv1, max_uv2 = $max_uv2, big_iv = $big_iv\n";
+print "# max_uv_less3 = $max_uv_less3\n";
+if ($max_uv1 ne $max_uv2 or $big_iv > $max_uv1 or $max_uv1 == $max_uv_less3) {
+  print "1..0 # skipped: unsigned perl arithmetic is not sane";
+  eval { require Config; import Config };
+  use vars qw(%Config);
+  if ($Config{d_quad} eq 'define') {
+      print " (common in 64-bit platforms)";
+  }
+  print "\n";
+  exit 0;
+}
+if ($max_uv_less3 =~ tr/0-9//c) {
+  print "1..0 # skipped: this perl stringifies large unsigned integers using E notation\n";
   exit 0;
 }
 
@@ -78,8 +91,24 @@ my @list = (1, $yet_smaller_than_iv, $smaller_than_iv, $max_iv, $max_iv + 1,
 unshift @list, (reverse map -$_, @list), 0; # 15 elts
 @list = map "$_", @list; # Normalize
 
-# print "@list\n";
+print "# @list\n";
+
+# need to special case ++ for max_uv, as ++ "magic" on a string gives
+# another string, whereas ++ magic on a string used as a number gives
+# a number. Not a problem when NV preserves UV, but if it doesn't then
+# stringification of the latter gives something in e notation.
 
+my $max_uv_pp = "$max_uv"; $max_uv_pp++;
+my $max_uv_p1 = "$max_uv"; $max_uv_p1+=0; $max_uv_p1++;
+
+# Also need to cope with %g notation for max_uv_p1 that actually gives an
+# integer less than max_uv because of correct rounding for the limited
+# precisision. This bites for 12 byte long doubles and 8 byte UVs
+
+my $temp = $max_uv_p1;
+my $max_uv_p1_as_iv;
+{use integer; $max_uv_p1_as_iv = 0 + sprintf "%s", $temp}
+my $max_uv_p1_as_uv = 0 | sprintf "%s", $temp;
 
 my @opnames = split //, "-+UINPuinp";
 
@@ -171,12 +200,56 @@ for my $num_chain (1..$max_chain) {
            }
            push @ans, $inpt;
          }
-         $nok++,
-           print "# '$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]\n"
-             if $ans[0] ne $ans[1];
+         if ($ans[0] ne $ans[1]) {
+           print "# '$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]\n";
+           # XXX ought to check that "+" was in the list of opnames
+           if ((($ans[0] eq $max_uv_pp) and ($ans[1] eq $max_uv_p1))
+               or (($ans[1] eq $max_uv_pp) and ($ans[0] eq $max_uv_p1))) {
+             # string ++ versus numeric ++. Tolerate this little
+             # bit of insanity
+             print "# ok, as string ++ of max_uv is \"$max_uv_pp\", numeric is $max_uv_p1\n"
+           } elsif ($opnames[$last] eq 'I' and $ans[1] eq "-1"
+                    and $ans[0] eq $max_uv_p1_as_iv) {
+              # Max UV plus 1 is NV. This NV may stringify in E notation.
+              # And the number of decimal digits shown in E notation will depend
+              # on the binary digits in the mantissa. And it may be that
+              # (say)  18446744073709551616 in E notation is truncated to
+              # (say) 1.8446744073709551e+19 (say) which gets converted back
+              # as    1.8446744073709551000e+19
+              # ie    18446744073709551000
+              # which isn't the integer we first had.
+              # But each step of conversion is correct. So it's not an error.
+              # (Only shows up for 64 bit UVs and NVs with 64 bit mantissas,
+              #  and on Crays (64 bit integers, 48 bit mantissas) IIRC)
+             print "# ok, \"$max_uv_p1\" correctly converts to IV \"$max_uv_p1_as_iv\"\n";
+           } elsif ($opnames[$last] eq 'U' and $ans[1] eq ~0
+                    and $ans[0] eq $max_uv_p1_as_uv) {
+              # as aboce
+             print "# ok, \"$max_uv_p1\" correctly converts to UV \"$max_uv_p1_as_uv\"\n";
+           } elsif (grep {/^N$/} @opnames[@{$curops[0]}]
+                    and $ans[0] == $ans[1] and $ans[0] <= ~0
+                     # First must be in E notation (ie not just digits) and
+                     # second must still be an integer.
+                    # eg 1.84467440737095516e+19
+                    # 1.84467440737095516e+19 for 64 bit mantissa is in the
+                    # integer range, so 1.84467440737095516e+19 + 0 is treated
+                    # as integer addition. [should it be?]
+                    # and 18446744073709551600 + 0 is 18446744073709551600
+                    # Which isn't the string you first thought of.
+                     # I can't remember why there isn't symmetry in this
+                     # exception, ie why only the first ops are tested for 'N'
+                     and $ans[0] != /^-?\d+$/ and $ans[1] !~ /^-?\d+$/) {
+             print "# ok, numerically equal - notation changed due to adding zero\n";
+           } else {
+             $nok++,
+           }
+         }
        }
-       print "not " if $nok;
-       print "ok $test\n";
+        if ($nok) {
+          print "not ok $test\n";
+        } else {
+          print "ok $test\n";
+        }
        #print $txt if $nok;
        $test++;
       }