~ chicken-core (master) 0e09338fb241ac0bf4dec9aa48c0f156103b0a58


commit 0e09338fb241ac0bf4dec9aa48c0f156103b0a58
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Thu Oct 15 15:30:53 2026 +0200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Thu Oct 15 16:18:30 2026 +0200

    Avoid accumulated errors in string to number conversion
    
    Converting the part before and after the decimal point to a floating
    point and adding them together accumulates errors.
    
    So now, when reading e.g. a number like "12.34e1", instead of
    calculating (+ 120.0 3.4), we calculate it as (expt (1200 + 34) -10).
    
    Signed-off-by: felix <felix@call-with-current-continuation.org>

diff --git a/NEWS b/NEWS
index c3596e37..195465f3 100644
--- a/NEWS
+++ b/NEWS
@@ -68,6 +68,9 @@
     used an obsolete calling convention.
   - exact->inexact on negative rational numbers now preserves the precision
     by rounding correctly (reported by "Juan")
+  - "string->number" and "read" now preserve precision by avoiding accumulated
+    flonum errors for inexact numerics with combined fraction and integral
+    components (reported by "Juan").
 
 - Syntax expander
   - Fixed bug in handling of "export-all" library declaration which caused
diff --git a/library.scm b/library.scm
index a5465a66..ae2e5879 100644
--- a/library.scm
+++ b/library.scm
@@ -3228,7 +3228,7 @@ EOF
                      (set! quo (round-quotient num (* scl 2)))))
               (ldexp (exact->inexact quo) bex))
             ;; Fall back to exact calculation in extreme cases
-            (* mant (##sys#integer-power 10 point))))))
+            (exact->inexact (* mant (##sys#integer-power 10 point)))))))
 
 (define ldexp (foreign-lambda double "ldexp" double int))
 
@@ -3247,17 +3247,18 @@ EOF
     (unless (eq? exactness 'e)
       (set! exactness 'i)
       (set! negative (or negative neg?))))
-  (define (safe-exponent value e)
+  (define (safe-exponent value e allow-inexact?)
     (and e (cond
             ((not value) 0)
             ((> e +maximum-allowed-exponent+)
-             (and (eq? exactness 'i)
+             (and allow-inexact?
+                  (eq? exactness 'i)
                   (cond ((zero? value) 0.0)
                         ((> value 0.0) +inf.0)
                         (else -inf.0))))
             ((< e (fxneg +maximum-allowed-exponent+))
-             (and (eq? exactness 'i) +0.0))
-            ((eq? exactness 'i) (mantexp->dbl value e))
+             (and allow-inexact? (eq? exactness 'i) +0.0))
+            ((and allow-inexact? (eq? exactness 'i)) (mantexp->dbl value e))
             (else (* value (##sys#integer-power 10 e))))))
   (define (make-nan)
     ;; Return fresh NaNs, so eqv? returns #f on two read NaNs.  This
@@ -3353,17 +3354,26 @@ EOF
                            (and-let* (((fx> len next))
                                       (ee (scan-exponent (fx+ next 1)))
                                       (e (car ee))
-                                      (h (safe-exponent decimal-head e)))
-                             (let* ((te (and tail (fx- e (fx- (cdr tail) start))))
-                                    (num (and tail (car tail)))
-                                    (t (safe-exponent num te)))
-                               (cons (if t (+ h t) h) (cdr ee)))))
-                          (else (let* ((last (or next len))
-                                       (te (and tail (fx- start last)))
-                                       (num (and tail (car tail)))
-                                       (t (safe-exponent num te))
-                                       (h (or decimal-head 0)))
-                                  (cons (if t (+ h t) h) next)))))))))
+                                      (val (if tail
+                                               (let* ((num (or (car tail) 0))
+                                                      (te (fx- e (fx- (cdr tail) start)))
+                                                      (h (if decimal-head
+                                                             (safe-exponent decimal-head (- e te) #f)
+                                                             0)))
+                                                 (safe-exponent (+ h num) te #t))
+                                               (safe-exponent decimal-head e #t))))
+                             (cons val (cdr ee))))
+                          (else (cons (if tail
+                                          (let* ((last (or next len))
+                                                 (te (fx- start last))
+                                                 (num (or (car tail) 0))
+                                                 (h (if decimal-head
+                                                        (safe-exponent decimal-head (- te) #f)
+                                                        0)))
+                                            (safe-exponent (+ h num) te #t))
+                                          ;; NOTE: May convert to inexact or +inf.0 etc, so this is not a no-op
+                                          (safe-exponent decimal-head 0 #t))
+                                      next))))))))
          (scan-ureal
           (lambda (start neg? cplx?)
             (if (and (fx> len (fx+ start 1)) (eq? radix 10)
@@ -3386,7 +3396,7 @@ EOF
                                 ((fx> len (cdr end)))
                                 (ee (scan-exponent (fx+ (cdr end) 1)))
                                 (num (car end))
-                                (val (safe-exponent num (car ee))))
+                                (val (safe-exponent num (car ee) #t)))
                        (cons val (cdr ee))))
                     ((#\/)
                      (set! seen-hashes? #f) ; Reset flag for denominator
diff --git a/tests/numbers-test.scm b/tests/numbers-test.scm
index 373fd0e5..85e1eb0e 100644
--- a/tests/numbers-test.scm
+++ b/tests/numbers-test.scm
@@ -1100,6 +1100,12 @@
    (test-equal "inexact are 0" +0.0 i1)
    (test-equal "exact are equal" e2 e1)
    (test-equal "exact are false" #f e1))
+ ;; However, we do need to keep exact numbers exact as long as we can
+ ;; to avoid accumulating floating point errors, as reported by "Juan"
+ (test-equal 5597974336821527/4503599627370496
+             (inexact->exact (string->number "1.243")))
+ (test-equal 5597974336821527/4503599627370496
+             (inexact->exact (exact->inexact 1243/1000)))
 
  (test-group "read/write invariance of simple integers for different radices"
    (let lp ((radix 2)
Trap