|
147 | 147 | (ormap identity as)) |
148 | 148 |
|
149 | 149 | (define (ival-pi) |
150 | | - (ival (endpoint (rnd 'down pi.bf) #f) (endpoint (rnd 'up pi.bf) #f) #f #f)) |
| 150 | + (define lo (mpfr-new! (bf-precision))) |
| 151 | + (define hi (mpfr-new! (bf-precision))) |
| 152 | + (mpfr-const-pi! lo 'down) |
| 153 | + (mpfr-const-pi! hi 'up) |
| 154 | + (ival (endpoint lo #f) (endpoint hi #f) #f #f)) |
151 | 155 |
|
152 | 156 | (define (ival-e) |
153 | | - (ival (endpoint (rnd 'down bfexp 1.bf) #f) (endpoint (rnd 'up bfexp 1.bf) #f) #f #f)) |
| 157 | + (define lo (mpfr-new! (bf-precision))) |
| 158 | + (define hi (mpfr-new! (bf-precision))) |
| 159 | + (mpfr-exp! lo 1.bf 'down) |
| 160 | + (mpfr-exp! hi 1.bf 'up) |
| 161 | + (ival (endpoint lo #f) (endpoint hi #f) #f #f)) |
154 | 162 |
|
155 | 163 | (define (ival-bool b) |
156 | 164 | (ival (endpoint b #t) (endpoint b #t) #f #f)) |
|
187 | 195 | [(bfnegative? (ival-hi-val x)) -1] |
188 | 196 | [else 0])) |
189 | 197 |
|
190 | | -(define (endpoint-min2 e1 e2) |
| 198 | +(define (endpoint-min2 e1 e2 rnd) |
191 | 199 | (match-define (endpoint x x!) e1) |
192 | 200 | (match-define (endpoint y y!) e2) |
193 | | - (define out (bfmin2 x y)) |
| 201 | + (define src (endpoint-val (if (bflte? x y) e1 e2))) |
| 202 | + (define out (mpfr-new! (bf-precision))) |
| 203 | + (mpfr-set! out src rnd) |
194 | 204 | (endpoint out (or (and (bf=? out x) x!) (and (bf=? out y) y!)))) |
195 | 205 |
|
196 | | -(define (endpoint-max2 e1 e2) |
| 206 | +(define (endpoint-max2 e1 e2 rnd) |
197 | 207 | (match-define (endpoint x x!) e1) |
198 | 208 | (match-define (endpoint y y!) e2) |
199 | | - (define out (bfmax2 x y)) |
| 209 | + (define src (endpoint-val (if (bfgte? x y) e1 e2))) |
| 210 | + (define out (mpfr-new! (bf-precision))) |
| 211 | + (mpfr-set! out src rnd) |
200 | 212 | (endpoint out (or (and (bf=? out x) x!) (and (bf=? out y) y!)))) |
201 | 213 |
|
202 | 214 | (define (ival-union x y) |
203 | 215 | (cond |
204 | 216 | [(ival-err x) (struct-copy ival y [err? #t])] |
205 | 217 | [(ival-err y) (struct-copy ival x [err? #t])] |
206 | 218 | [(bigfloat? (ival-lo-val x)) |
207 | | - (ival (rnd 'down endpoint-min2 (ival-lo x) (ival-lo y)) |
208 | | - (rnd 'up endpoint-max2 (ival-hi x) (ival-hi y)) |
| 219 | + (ival (endpoint-min2 (ival-lo x) (ival-lo y) 'down) |
| 220 | + (endpoint-max2 (ival-hi x) (ival-hi y) 'up) |
209 | 221 | (or (ival-err? x) (ival-err? y)) |
210 | 222 | (and (ival-err x) (ival-err y)))] |
211 | 223 | [(boolean? (ival-lo-val x)) |
|
226 | 238 | (define exact? (= 0 (mpfr-fun! out a rnd))) |
227 | 239 | (endpoint out (and a! exact?))) |
228 | 240 |
|
| 241 | +(define (epbinary! out mpfr-fun! a-endpoint b-endpoint rnd) |
| 242 | + (match-define (endpoint a a!) a-endpoint) |
| 243 | + (match-define (endpoint b b!) b-endpoint) |
| 244 | + (mpfr-set-prec! out (bf-precision)) |
| 245 | + (define exact? (= 0 (mpfr-fun! out a b rnd))) |
| 246 | + (endpoint out (and a! b! exact?))) |
| 247 | + |
229 | 248 | (define ((monotonic-mpfr mpfr-fn!) x) |
230 | 249 | (match-define (ival lo hi err? err) x) |
231 | 250 | (define out (new-ival)) |
|
279 | 298 | (or xerr? (bflte? xlo lo) (bfgte? xhi hi)) |
280 | 299 | (or xerr (bflte? xhi lo) (bfgte? xlo hi)))) |
281 | 300 |
|
282 | | -;; TODO: rewrite this for new functions |
283 | 301 | (define ((overflows-at fn lo hi) x) |
284 | 302 | (match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x) |
285 | 303 | (match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) yerr? yerr) (fn x)) |
|
316 | 334 | (f x))) |
317 | 335 |
|
318 | 336 | (define* ival-rint (monotonic-mpfr mpfr-rint!)) |
319 | | -;; TODO: move these to monotonic-mpfr |
320 | 337 | (define* ival-round (monotonic (fix-rounding bfround))) |
321 | 338 | (define* ival-ceil (monotonic (fix-rounding bfceiling))) |
322 | 339 | (define* ival-floor (monotonic (fix-rounding bffloor))) |
|
332 | 349 | (define tmp2 (mpfr-new! (bf-precision))) |
333 | 350 | (define abs-lo (epunary! tmp1 mpfr-abs! (ival-lo x) 'up)) |
334 | 351 | (define abs-hi (epunary! tmp2 mpfr-abs! (ival-hi x) 'up)) |
335 | | - (ival (endpoint (bf 0) (and xlo! xhi!)) (endpoint-max2 abs-lo abs-hi) xerr? xerr)])) |
| 352 | + (ival (endpoint (bf 0) (and xlo! xhi!)) (endpoint-max2 abs-lo abs-hi 'nearest) xerr? xerr)])) |
336 | 353 |
|
337 | 354 | ;; These functions execute ival-fabs and ival-neg with input's precision |
338 | 355 | (define (ival-max-prec x) |
339 | 356 | (max (bigfloat-precision (ival-lo-val x)) (bigfloat-precision (ival-hi-val x)))) |
340 | 357 |
|
| 358 | +;; TODO: Cleanup, use begin1 |
341 | 359 | (define (ival-exact-fabs x) |
342 | | - (parameterize ([bf-precision (ival-max-prec x)]) |
343 | | - (ival-fabs x))) |
| 360 | + (define saved-prec (bf-precision)) |
| 361 | + (bf-precision (ival-max-prec x)) |
| 362 | + (define result (ival-fabs x)) |
| 363 | + (bf-precision saved-prec) |
| 364 | + result) |
344 | 365 |
|
345 | 366 | (define (ival-exact-neg x) |
346 | | - (parameterize ([bf-precision (ival-max-prec x)]) |
347 | | - (ival-neg x))) |
| 367 | + (define saved-prec (bf-precision)) |
| 368 | + (bf-precision (ival-max-prec x)) |
| 369 | + (define result (ival-neg x)) |
| 370 | + (bf-precision saved-prec) |
| 371 | + result) |
348 | 372 |
|
349 | 373 | ;; Since MPFR has a cap on exponents, no value can be more than twice MAX_VAL |
350 | 374 | (define exp-overflow-threshold (bfadd (bflog (bfprev +inf.bf)) 1.bf)) |
|
398 | 422 | (define err (or (ival-err x) (ival-err y))) |
399 | 423 |
|
400 | 424 | (define (mkatan a b c d) |
401 | | - (ival (rnd 'down epfn bfatan2 a b) (rnd 'up epfn bfatan2 c d) err? err)) |
| 425 | + (define lo-out (mpfr-new! (bf-precision))) |
| 426 | + (define hi-out (mpfr-new! (bf-precision))) |
| 427 | + (ival (epbinary! lo-out mpfr-atan2! a b 'down) (epbinary! hi-out mpfr-atan2! c d 'up) err? err)) |
402 | 428 |
|
403 | 429 | (match* ((classify-ival-strict x) (classify-ival-strict y)) |
404 | 430 | [(-1 -1) (mkatan yhi xlo ylo xhi)] |
|
409 | 435 | [(0 1) (mkatan ylo xhi ylo xlo)] |
410 | 436 | [(-1 1) (mkatan yhi xhi ylo xlo)] |
411 | 437 | [(_ 0) |
412 | | - (ival (endpoint (bfneg (rnd 'up pi.bf)) #f) |
413 | | - (endpoint (rnd 'up pi.bf) #f) |
| 438 | + (define pi-int (ival-pi)) |
| 439 | + (define hi-out (mpfr-new! (bf-precision))) |
| 440 | + (define lo-out (mpfr-new! (bf-precision))) |
| 441 | + (mpfr-set! hi-out (endpoint-val (ival-hi pi-int)) 'up) |
| 442 | + (mpfr-neg! lo-out (endpoint-val (ival-hi pi-int)) 'down) |
| 443 | + (ival (endpoint lo-out #f) |
| 444 | + (endpoint hi-out #f) |
414 | 445 | (or err? (bfgte? (ival-hi-val x) 0.bf)) |
415 | 446 | (or err |
416 | 447 | (and (bfzero? (ival-lo-val x)) |
417 | 448 | (bfzero? (ival-hi-val x)) |
418 | 449 | (bfzero? (ival-lo-val y)) |
419 | 450 | (bfzero? (ival-hi-val y)))))])) |
420 | 451 |
|
421 | | -(define* |
422 | | - ival-cosh |
423 | | - (compose (overflows-at (monotonic-mpfr mpfr-cosh!) (bfneg acosh-overflow-threshold) acosh-overflow-threshold) |
424 | | - ival-exact-fabs)) |
| 452 | +(define* ival-cosh |
| 453 | + (compose (overflows-at (monotonic-mpfr mpfr-cosh!) |
| 454 | + (bfneg acosh-overflow-threshold) |
| 455 | + acosh-overflow-threshold) |
| 456 | + ival-exact-fabs)) |
425 | 457 | (define* |
426 | 458 | ival-sinh |
427 | 459 | (overflows-at (monotonic-mpfr mpfr-sinh!) (bfneg sinh-overflow-threshold) sinh-overflow-threshold)) |
|
534 | 566 | (ival (endpoint lo #f) (endpoint hi #f) err? err)) |
535 | 567 |
|
536 | 568 | (define (ival-fmin x y) |
537 | | - (ival (rnd 'down endpoint-min2 (ival-lo x) (ival-lo y)) |
538 | | - (rnd 'up endpoint-min2 (ival-hi x) (ival-hi y)) |
| 569 | + (ival (endpoint-min2 (ival-lo x) (ival-lo y) 'down) |
| 570 | + (endpoint-min2 (ival-hi x) (ival-hi y) 'up) |
539 | 571 | (or (ival-err? x) (ival-err? y)) |
540 | 572 | (or (ival-err x) (ival-err y)))) |
541 | 573 |
|
542 | 574 | (define (ival-fmax x y) |
543 | | - (ival (rnd 'down endpoint-max2 (ival-lo x) (ival-lo y)) |
544 | | - (rnd 'up endpoint-max2 (ival-hi x) (ival-hi y)) |
| 575 | + (ival (endpoint-max2 (ival-lo x) (ival-lo y) 'down) |
| 576 | + (endpoint-max2 (ival-hi x) (ival-hi y) 'up) |
545 | 577 | (or (ival-err? x) (ival-err? y)) |
546 | 578 | (or (ival-err x) (ival-err y)))) |
547 | 579 |
|
|
0 commit comments