Skip to content

Commit 3daf13b

Browse files
authored
Avoid using mpfr_rint_roundeven (racket#91)
* Fix bfround, bfceiling, and bftruncate * Add a test that the rounding operations are fixed * Match Racket rounding behavior * Fix bfround
1 parent bb3e1bb commit 3daf13b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

math-lib/math/private/bigfloat/mpfr.rkt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ There's no reason to allocate new limbs for an _mpfr without changing its precis
687687
[bfbesy0 'mpfr_y0]
688688
[bfbesy1 'mpfr_y1]
689689
[bfrint 'mpfr_rint]
690-
[bfround 'mpfr_rint_roundeven]
691690
[bffloor 'mpfr_rint_floor]
692691
[bfceiling 'mpfr_rint_ceil]
693692
[bftruncate 'mpfr_rint_trunc]
@@ -702,9 +701,20 @@ There's no reason to allocate new limbs for an _mpfr without changing its precis
702701
[(= 0 (bigfloat-signbit x)) (force 1.bf)]
703702
[else (force -1.bf)]))
704703

705-
(provide bfsgn)
704+
(define (bfround x)
705+
(if (>= (bigfloat-exponent x) 0)
706+
;; In this case, `x` is already an integer, so no rounding needs
707+
;; to be performed; we use bfcopy to change precision while
708+
;; respecting rounding mode
709+
(bfcopy x)
710+
;; In this case, `x` is known not to be near infinity, so
711+
;; `bfrint` will do the right thing.
712+
(parameterize ([bf-rounding-mode 'nearest])
713+
(bfrint x))))
714+
715+
(provide bfsgn bfround)
706716
(begin-for-syntax
707-
(set! 1ary-funs (list* #'bfsgn 1ary-funs)))
717+
(set! 1ary-funs (list* #'bfsgn #'bfround 1ary-funs)))
708718

709719
(define mpfr-fac-ui (get-mpfr-fun 'mpfr_fac_ui (_fun _mpfr-pointer _ulong _rnd_t -> _int)))
710720

0 commit comments

Comments
 (0)