~ chicken-core (master) 213eb00ccd48c7b1008bca1dbee1a2bb89bea38f


commit 213eb00ccd48c7b1008bca1dbee1a2bb89bea38f
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Thu Oct 15 10:05:04 2026 +0200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Thu Oct 15 13:51:35 2026 +0200

    Fix rounding in exact->inexact on negative ratnums
    
    The code assumed that both the numerator and the denominator were
    always positive, caused by a missed call to "abs" on the numerator
    when translating this code from the numbers egg's Scheme code to C.
    
    Signed-off-by: felix <felix@call-with-current-continuation.org>

diff --git a/NEWS b/NEWS
index 56841f6b..c3596e37 100644
--- a/NEWS
+++ b/NEWS
@@ -66,6 +66,8 @@
     please use the corresponding operations from "(scheme process-context)".
   - Corrected use of "make-output-port" in "make-broadcast-port", which 
     used an obsolete calling convention.
+  - exact->inexact on negative rational numbers now preserves the precision
+    by rounding correctly (reported by "Juan")
 
 - Syntax expander
   - Fixed bug in handling of "export-all" library declaration which caused
diff --git a/manual/Acknowledgements b/manual/Acknowledgements
index 9e263881..bded1deb 100644
--- a/manual/Acknowledgements
+++ b/manual/Acknowledgements
@@ -25,7 +25,7 @@ itojun Hagino, Ahdi Hargo, Matthias Heiler, Karl M. Hegbloom, Moritz Heidkamp,
 William P. Heinemann, Christian Himpe, Bill Hoffman, Eric Hoffman, Bruce Hoult,
 Hans Hübner,
 Markus Hülsmann, Anton Idukov, Götz Isenmann, Paulo Jabardo, Wietse Jacobs, David Janssens,
-Christian Jäger, Robert Jensen, "Johnjaye", Matt Jones, Dale Jordan, Valentin Kamyshenko,
+Christian Jäger, Robert Jensen, "Johnjaye", Matt Jones, Dale Jordan, "Juan", Valentin Kamyshenko,
 Daishi Kato, Peter Keller, Christian Kellermann, Brad Kind, Stanislav Kljuhhin, Ron Kneusel, "Kooda",
 Matthias Köppe, Krysztof Kowalczyk, Andre Kühne, Todd R. Kueny Sr, Goran Krampe,
 David Krentzlin, Ben Kurtz, Sebastien Marie, Alice Maz, Michele La Monaca,
diff --git a/runtime.c b/runtime.c
index a62f2297..0d6b8437 100644
--- a/runtime.c
+++ b/runtime.c
@@ -10769,14 +10769,22 @@ C_a_i_exact_to_inexact(C_word **ptr, int c, C_word n)
               * e_w in M2.  TODO: What if b!=2 (ie, flonum-radix isn't 2)?
               */
              e = integer_length_abs(num) - integer_length_abs(denom),
-             ab[C_SIZEOF_FIX_BIGNUM*5+C_SIZEOF_FLONUM], *a = ab, tmp, q, r, len,
+             ab[C_SIZEOF_FIX_BIGNUM*6+C_SIZEOF_FLONUM], *a = ab, tmp, q, r, len,
              shift_amount, negp = C_i_integer_negativep(num);
      C_uword *d;
      double res, fraction;
 
+     /* From here on, calculate with a positive numerator (will restore sign at the end) */
+     if (C_truep(negp)) num = C_s_a_u_i_integer_abs(&a, 1, num);
+
      /* Align by shifting the smaller to the size of the larger */
-     if (e < 0)      num = C_s_a_i_arithmetic_shift(&a, 2, num, C_fix(-e));
-     else if (e > 0) denom = C_s_a_i_arithmetic_shift(&a, 2, denom, C_fix(e));
+     if (e < 0) {
+       tmp = C_s_a_i_arithmetic_shift(&a, 2, num, C_fix(-e));
+       clear_buffer_object(ab, num); /* "knows" shift creates fresh numbers */
+       num = tmp;
+     } else if (e > 0) {
+       denom = C_s_a_i_arithmetic_shift(&a, 2, denom, C_fix(e));
+     }
 
      /* Here, 1/2 <= n/d < 2 [N3] */
      if (C_truep(C_i_integer_lessp(num, denom))) { /* n/d < 1? */
@@ -10798,7 +10806,7 @@ C_a_i_exact_to_inexact(C_word **ptr, int c, C_word n)
 
      /* We multiply the remainder by two to simulate adding 1/2 for
       * round.  However, we don't do it if num = denom (q=1,r=0) */
-     if (!((q == C_fix(1) || q == C_fix(-1)) && r == C_fix(0))) {
+     if (!(q == C_fix(1) && r == C_fix(0))) {
        tmp = C_s_a_i_arithmetic_shift(&a, 2, r, C_fix(1));
        clear_buffer_object(ab, r); /* "knows" shift creates fresh numbers */
        r = tmp;
@@ -10810,7 +10818,7 @@ C_a_i_exact_to_inexact(C_word **ptr, int c, C_word n)
       * case of exactly halfway, we round up if odd.
       */
      tmp = C_a_i_exact_to_inexact(&a, 1, q);
-     fraction = fabs(C_flonum_magnitude(tmp));
+     fraction = C_flonum_magnitude(tmp);
      switch (basic_cmp(r, denom, "", 0)) {
      case C_fix(0):
        if (C_truep(C_i_oddp(q))) fraction += 1.0;
diff --git a/tests/numbers-test.scm b/tests/numbers-test.scm
index ec6ff494..373fd0e5 100644
--- a/tests/numbers-test.scm
+++ b/tests/numbers-test.scm
@@ -726,6 +726,8 @@
  (test-error "inexact->exact +inf" (inexact->exact +inf.0))
  (test-error "inexact->exact -inf" (inexact->exact -inf.0))
  (test-error "inexact->exact -NaN" (inexact->exact +nan.0))
+ (test-assert "exact->inexact preserves precision"
+              (= (exact->inexact -1/11) (- (exact->inexact 1/11))))
  (test-equal "sqrt (integer result)" (sqrt 16) 4)
  (test-equal "sqrt (exact result)" (sqrt 1/4) 1/2)
  (parameterize ((current-test-epsilon 1e-10))
Trap