|
121 | 121 | (define (ival-hi-fixed? ival) |
122 | 122 | (endpoint-immovable? (ival-hi ival))) |
123 | 123 |
|
| 124 | +(define (make-endpoint-pair) |
| 125 | + (values (mpfr-new! (bf-precision)) (mpfr-new! (bf-precision)))) |
| 126 | + |
124 | 127 | (define (new-ival) |
125 | 128 | ; Warning, leaks memory unless `mpfr-clear!` called eventually |
126 | | - (define bf1 (mpfr-new! (bf-precision))) |
127 | | - (define bf2 (mpfr-new! (bf-precision))) |
| 129 | + (define-values (bf1 bf2) (make-endpoint-pair)) |
128 | 130 | (ival (endpoint bf1 #f) (endpoint bf2 #f) #f #f)) |
129 | 131 |
|
130 | 132 | (define (mk-big-ival x y) |
|
147 | 149 | (ormap identity as)) |
148 | 150 |
|
149 | 151 | (define (ival-pi) |
150 | | - (define lo (mpfr-new! (bf-precision))) |
151 | | - (define hi (mpfr-new! (bf-precision))) |
| 152 | + (define-values (lo hi) (make-endpoint-pair)) |
152 | 153 | (mpfr-const-pi! lo 'down) |
153 | 154 | (mpfr-const-pi! hi 'up) |
154 | 155 | (ival (endpoint lo #f) (endpoint hi #f) #f #f)) |
155 | 156 |
|
156 | 157 | (define (ival-e) |
157 | | - (define lo (mpfr-new! (bf-precision))) |
158 | | - (define hi (mpfr-new! (bf-precision))) |
| 158 | + (define-values (lo hi) (make-endpoint-pair)) |
159 | 159 | (mpfr-exp! lo 1.bf 'down) |
160 | 160 | (mpfr-exp! hi 1.bf 'up) |
161 | 161 | (ival (endpoint lo #f) (endpoint hi #f) #f #f)) |
|
345 | 345 | [1 ((monotonic-mpfr mpfr-abs!) x)] |
346 | 346 | [0 |
347 | 347 | (match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x) |
348 | | - (define tmp1 (mpfr-new! (bf-precision))) |
349 | | - (define tmp2 (mpfr-new! (bf-precision))) |
| 348 | + (define-values (tmp1 tmp2) (make-endpoint-pair)) |
350 | 349 | (define abs-lo (epunary! tmp1 mpfr-abs! (ival-lo x) 'up)) |
351 | 350 | (define abs-hi (epunary! tmp2 mpfr-abs! (ival-hi x) 'up)) |
352 | 351 | (ival (endpoint (bf 0) (and xlo! xhi!)) (endpoint-max2 abs-lo abs-hi 'nearest) xerr? xerr)])) |
|
355 | 354 | (define (ival-max-prec x) |
356 | 355 | (max (bigfloat-precision (ival-lo-val x)) (bigfloat-precision (ival-hi-val x)))) |
357 | 356 |
|
358 | | -;; TODO: Cleanup, use begin1 |
359 | 357 | (define (ival-exact-fabs x) |
360 | 358 | (define saved-prec (bf-precision)) |
361 | 359 | (bf-precision (ival-max-prec x)) |
362 | | - (define result (ival-fabs x)) |
363 | | - (bf-precision saved-prec) |
364 | | - result) |
| 360 | + (begin0 (ival-fabs x) |
| 361 | + (bf-precision saved-prec))) |
365 | 362 |
|
366 | 363 | (define (ival-exact-neg x) |
367 | 364 | (define saved-prec (bf-precision)) |
368 | 365 | (bf-precision (ival-max-prec x)) |
369 | | - (define result (ival-neg x)) |
370 | | - (bf-precision saved-prec) |
371 | | - result) |
| 366 | + (begin0 (ival-neg x) |
| 367 | + (bf-precision saved-prec))) |
372 | 368 |
|
373 | 369 | ;; Since MPFR has a cap on exponents, no value can be more than twice MAX_VAL |
374 | 370 | (define exp-overflow-threshold (bfadd (bflog (bfprev +inf.bf)) 1.bf)) |
|
422 | 418 | (define err (or (ival-err x) (ival-err y))) |
423 | 419 |
|
424 | 420 | (define (mkatan a b c d) |
425 | | - (define lo-out (mpfr-new! (bf-precision))) |
426 | | - (define hi-out (mpfr-new! (bf-precision))) |
| 421 | + (define-values (lo-out hi-out) (make-endpoint-pair)) |
427 | 422 | (ival (epbinary! lo-out mpfr-atan2! a b 'down) (epbinary! hi-out mpfr-atan2! c d 'up) err? err)) |
428 | 423 |
|
429 | 424 | (match* ((classify-ival-strict x) (classify-ival-strict y)) |
|
436 | 431 | [(-1 1) (mkatan yhi xhi ylo xlo)] |
437 | 432 | [(_ 0) |
438 | 433 | (define pi-int (ival-pi)) |
439 | | - (define hi-out (mpfr-new! (bf-precision))) |
440 | | - (define lo-out (mpfr-new! (bf-precision))) |
| 434 | + (define-values (hi-out lo-out) (make-endpoint-pair)) |
441 | 435 | (mpfr-set! hi-out (endpoint-val (ival-hi pi-int)) 'up) |
442 | 436 | (mpfr-neg! lo-out (endpoint-val (ival-hi pi-int)) 'down) |
443 | 437 | (ival (endpoint lo-out #f) |
|
0 commit comments