From 5cb02282f5f7086280ac3d91b6451f7b12b86b07 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Sun, 22 Nov 2015 18:09:11 -0700 Subject: [PATCH] fix round-to-even for `exact->inexact` on rationals Thanks again to Robby --- pkgs/racket-test-core/tests/racket/number.rktl | 10 ++++++++++ racket/src/racket/src/ratfloat.inc | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkgs/racket-test-core/tests/racket/number.rktl b/pkgs/racket-test-core/tests/racket/number.rktl index a00c9724a6..d601b473e1 100644 --- a/pkgs/racket-test-core/tests/racket/number.rktl +++ b/pkgs/racket-test-core/tests/racket/number.rktl @@ -3286,6 +3286,16 @@ (arithmetic-shift (random (expt 2 24)) 24) (random (expt 2 28))))) +;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Check roun-to-even of rationale conversion (thanks to Robby) + +(let () + (define l (floating-point-bytes->real #"\x1a\xd8\x9c\x17\x21\x2e\xfd\x25" #t)) + (define r (floating-point-bytes->real #"\x1a\xd8\x9c\x17\x21\x2e\xfd\x26" #t)) + (test r exact->inexact (/ (+ (inexact->exact l) + (inexact->exact r)) + 2))) + ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; exact->inexact precision (thanks to Neil Toronto) diff --git a/racket/src/racket/src/ratfloat.inc b/racket/src/racket/src/ratfloat.inc index 560b3eaaa4..9735c6c687 100644 --- a/racket/src/racket/src/ratfloat.inc +++ b/racket/src/racket/src/ratfloat.inc @@ -104,7 +104,7 @@ FP_TYPE SCHEME_RATIONAL_TO_FLOAT(const Scheme_Object *o) if (shift > FLOAT_M_BITS) { shift = FLOAT_M_BITS; } - + a[0] = n; a[1] = scheme_make_integer(shift); n = scheme_bitwise_shift(2, a); @@ -120,9 +120,9 @@ FP_TYPE SCHEME_RATIONAL_TO_FLOAT(const Scheme_Object *o) } else { /* Round to even: */ a[0] = d; - if (!scheme_odd_p(1, a)) { + if (SCHEME_FALSEP(scheme_odd_p(1, a))) { a[0] = n; - if (!scheme_even_p(1, a)) { + if (SCHEME_FALSEP(scheme_even_p(1, a))) { n = scheme_bin_plus(n, scheme_make_integer(1)); } }