develooper Front page | perl.vmsperl | Postings from June 2002

Re: [PATCH #2] Re: [PATCH] numeric.c:S_mulexp10 -- quit when you can

Thread Previous | Thread Next
From:
Hugo van der Sanden
Date:
June 19, 2002 07:25
Subject:
Re: [PATCH #2] Re: [PATCH] numeric.c:S_mulexp10 -- quit when you can
Message ID:
200206191427.g5JERgH10292@crypt.compulink.co.uk
"Craig A. Berry" <craigberry@mac.com> wrote:
:Urk.  I found a case where this isn't right (or actually
:lib/Math/Trig.t found a case where it isn't right):
:
:$ perl -e "my $x = 100.00000000000000001; print $x;"
:100
:
:so far so good, but now add another zero after the decimal:
:
:$ perl -e "my $x = 100.000000000000000001; print $x;"
:0

Could you try the patch below? It doesn't break anything here, but
I can't easily coerce my platform to use the new codepath.

I can't remember if C offers anything closer to a perlish 'redo'
than the <--i; continue;> used below.

Hugo
--- numeric.c.old	Sun Jun  9 18:55:23 2002
+++ numeric.c	Wed Jun 19 15:24:12 2002
@@ -902,9 +902,22 @@
     }
 
     /* combine components of mantissa */
-    for (i = 0; i <= ipart; ++i)
-	result += S_mulexp10((NV)part[ipart - i],
+    for (i = 0; i <= ipart; ++i) {
+	NV testresult = S_mulexp10((NV)part[ipart - i],
 		i ? offcount + (i - 1) * PARTSIZE : 0);
+	if (testresult == 0.0 && part[ipart - i] != 0 && expextra < 0) {
+	    /* since we calculate 100.001 as '100001 / 1000' we are in
+	       danger of rounding to 0.0 rather than 100.0 if the
+	       numerator overflows. Catch this, reduce precision, and redo
+	    */
+	    result /= 10.0;
+	    --offcount;
+	    ++expextra;
+	    --i;
+	    continue;
+	}
+	result += testresult;
+    }
 
     if (seendigit && (*s == 'e' || *s == 'E')) {
 	bool expnegative = 0;

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About