Skip to content

Commit 55d3c17

Browse files
committed
Implement ival-pre-fabs for hypot and pow2
1 parent bcda3ea commit 55d3c17

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

mpfr.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585

8686
(define mpfr-abs! (get-mpfr-fun 'mpfr_abs (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
8787

88+
(define mpfr-cmpabs (get-mpfr-fun 'mpfr_cmpabs (_fun _mpfr-pointer _mpfr-pointer -> _int)))
89+
8890
(define mpfr-asin! (get-mpfr-fun 'mpfr_asin (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
8991

9092
(define mpfr-acos! (get-mpfr-fun 'mpfr_acos (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
@@ -316,6 +318,7 @@
316318
mpfr-cbrt!
317319
mpfr-neg!
318320
mpfr-abs!
321+
mpfr-cmpabs
319322
mpfr-asin!
320323
mpfr-acos!
321324
mpfr-atan!

ops/arith.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@
161161
(define (ival-hypot! out x y)
162162
(define err? (or (ival-err? x) (ival-err? y)))
163163
(define err (or (ival-err x) (ival-err y)))
164-
(define x* (ival-exact-fabs x))
165-
(define y* (ival-exact-fabs y))
164+
(define x* (ival-pre-fabs x))
165+
(define y* (ival-pre-fabs y))
166166
(ival (eplinear! (ival-lo-val out) mpfr-hypot! (ival-lo x*) (ival-lo y*) 'down)
167167
(eplinear! (ival-hi-val out) mpfr-hypot! (ival-hi x*) (ival-hi y*) 'up)
168168
err?

ops/core.rkt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ival-max-prec
2020
ival-exact-neg
2121
ival-exact-fabs
22+
ival-pre-fabs
2223
bf-return-exact?
2324
ival-lo-fixed?
2425
ival-hi-fixed?
@@ -343,6 +344,19 @@
343344
(define abs-hi (epunary! tmp2 mpfr-abs! (ival-hi x) 'up))
344345
(ival (endpoint (bf 0) (and xlo! xhi!)) (endpoint-max2 abs-lo abs-hi 'up) xerr? xerr)]))
345346

347+
(define (ival-pre-fabs x)
348+
(match-define (ival xlo xhi xerr? xerr) x)
349+
(define match-result
350+
(match (classify-ival x)
351+
[1 x]
352+
[-1 (ival xhi xlo xerr? xerr)]
353+
[0
354+
(ival (endpoint 0.bf (and (endpoint-immovable? xlo) (endpoint-immovable? xhi)))
355+
(if (= (mpfr-cmpabs (endpoint-val xlo) (endpoint-val xhi)) 1) xlo xhi)
356+
xerr?
357+
xerr)]))
358+
match-result)
359+
346360
;; These functions execute ival-fabs and ival-neg with input's precision
347361
(define (ival-max-prec x)
348362
(max (bigfloat-precision (ival-lo-val x)) (bigfloat-precision (ival-hi-val x))))

ops/pow.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
(ival-then (ival-assert ival-maybe) (ival-union (ival-neg pospow) pospow)))))
140140

141141
(define (ival-pow2 x)
142-
((monotonic->ival (lambda (x) (bfmul x x))) (ival-exact-fabs x)))
142+
((monotonic->ival (lambda (x) (bfmul x x))) (ival-pre-fabs x)))
143143

144144
(define (ival-pow x y)
145145
(cond

0 commit comments

Comments
 (0)