|
285 | 285 | (define-syntax-rule (define* name expr) |
286 | 286 | (define name (procedure-rename expr 'name))) |
287 | 287 |
|
| 288 | +(define ((immutable fn!) x) |
| 289 | + (fn! (new-ival (bf-precision)) x)) |
| 290 | + |
288 | 291 | (define ((monotonic bffn) x) |
289 | 292 | (match-define (ival lo hi err? err) x) |
290 | 293 | (ival (rnd 'down epfn bffn lo) (rnd 'up epfn bffn hi) err? err)) |
|
354 | 357 | yerr)) |
355 | 358 |
|
356 | 359 | (define* ival-neg! (comonotonic-mpfr! mpfr-neg!)) |
357 | | -(define (ival-neg x) |
358 | | - (ival-neg! (new-ival (bf-precision)) x)) |
| 360 | +(define* ival-neg (immutable ival-neg!)) |
359 | 361 |
|
360 | 362 | ;; This function fixes a bug in MPFR where mixed-precision |
361 | 363 | ;; rint/round/ceil/floor/trunc operations are rounded in the input |
|
367 | 369 | (f x))) |
368 | 370 |
|
369 | 371 | (define* ival-rint! (monotonic-mpfr! mpfr-rint!)) |
370 | | -(define (ival-rint x) |
371 | | - (ival-rint! (new-ival (bf-precision)) x)) |
| 372 | +(define* ival-rint (immutable ival-rint!)) |
372 | 373 | (define* ival-round (monotonic (fix-rounding bfround))) |
373 | 374 | (define* ival-ceil (monotonic (fix-rounding bfceiling))) |
374 | 375 | (define* ival-floor (monotonic (fix-rounding bffloor))) |
|
406 | 407 | (define (ival-exp! out x) |
407 | 408 | (define y ((monotonic-mpfr! mpfr-exp!) out x)) |
408 | 409 | ((overflows-loose-at (bfneg exp-overflow-threshold) exp-overflow-threshold) x y)) |
409 | | - |
410 | | -(define (ival-exp x) |
411 | | - (ival-exp! (new-ival (bf-precision)) x)) |
| 410 | +(define* ival-exp (immutable ival-exp!)) |
412 | 411 |
|
413 | 412 | (define* |
414 | 413 | ival-expm1! |
415 | 414 | (overflows-at! (monotonic-mpfr! mpfr-expm1!) (bfneg exp-overflow-threshold) exp-overflow-threshold)) |
416 | | -(define (ival-expm1 x) |
417 | | - (ival-expm1! (new-ival (bf-precision)) x)) |
| 415 | +(define* ival-expm1 (immutable ival-expm1!)) |
418 | 416 |
|
419 | 417 | (define (ival-exp2! out x) |
420 | 418 | (define y ((monotonic-mpfr! mpfr-exp2!) out x)) |
421 | 419 | ((overflows-loose-at (bfneg exp2-overflow-threshold) exp2-overflow-threshold) x y)) |
422 | | - |
423 | | -(define (ival-exp2 x) |
424 | | - (ival-exp2! (new-ival (bf-precision)) x)) |
| 420 | +(define* ival-exp2 (immutable ival-exp2!)) |
425 | 421 |
|
426 | 422 | (define* ival-log! (clamp-strict! (monotonic-mpfr! mpfr-log!) 0.bf +inf.bf)) |
427 | | -(define (ival-log x) |
428 | | - (ival-log! (new-ival (bf-precision)) x)) |
| 423 | +(define* ival-log (immutable ival-log!)) |
429 | 424 | (define* ival-log2! (clamp-strict! (monotonic-mpfr! mpfr-log2!) 0.bf +inf.bf)) |
430 | | -(define (ival-log2 x) |
431 | | - (ival-log2! (new-ival (bf-precision)) x)) |
| 425 | +(define* ival-log2 (immutable ival-log2!)) |
432 | 426 | (define* ival-log10! (clamp-strict! (monotonic-mpfr! mpfr-log10!) 0.bf +inf.bf)) |
433 | | -(define (ival-log10 x) |
434 | | - (ival-log10! (new-ival (bf-precision)) x)) |
| 427 | +(define* ival-log10 (immutable ival-log10!)) |
435 | 428 | (define* ival-log1p! (clamp-strict! (monotonic-mpfr! mpfr-log1p!) -1.bf +inf.bf)) |
436 | | -(define (ival-log1p x) |
437 | | - (ival-log1p! (new-ival (bf-precision)) x)) |
| 429 | +(define* ival-log1p (immutable ival-log1p!)) |
438 | 430 | [define* ival-logb (compose ival-floor ival-log2 ival-exact-fabs)] |
439 | 431 |
|
440 | 432 | (define* ival-sqrt! (clamp! (monotonic-mpfr! mpfr-sqrt!) 0.bf +inf.bf)) |
441 | | -(define (ival-sqrt x) |
442 | | - (ival-sqrt! (new-ival (bf-precision)) x)) |
| 433 | +(define* ival-sqrt (immutable ival-sqrt!)) |
443 | 434 | (define* ival-cbrt! (monotonic-mpfr! mpfr-cbrt!)) |
444 | | -(define (ival-cbrt x) |
445 | | - (ival-cbrt! (new-ival (bf-precision)) x)) |
| 435 | +(define* ival-cbrt (immutable ival-cbrt!)) |
446 | 436 |
|
447 | 437 | (define (ival-and . as) |
448 | 438 | (ival (endpoint (andmap ival-lo-val as) (andmap (compose endpoint-immovable? ival-lo) as)) |
|
460 | 450 | (ival (epfn not (ival-hi x)) (epfn not (ival-lo x)) (ival-err? x) (ival-err x))) |
461 | 451 |
|
462 | 452 | (define* ival-asin! (clamp! (monotonic-mpfr! mpfr-asin!) -1.bf 1.bf)) |
463 | | -(define (ival-asin x) |
464 | | - (ival-asin! (new-ival (bf-precision)) x)) |
| 453 | +(define* ival-asin (immutable ival-asin!)) |
465 | 454 | (define* ival-acos! (clamp! (comonotonic-mpfr! mpfr-acos!) -1.bf 1.bf)) |
466 | | -(define (ival-acos x) |
467 | | - (ival-acos! (new-ival (bf-precision)) x)) |
| 455 | +(define* ival-acos (immutable ival-acos!)) |
468 | 456 |
|
469 | 457 | (define* ival-atan! (monotonic-mpfr! mpfr-atan!)) |
470 | | -(define (ival-atan x) |
471 | | - (ival-atan! (new-ival (bf-precision)) x)) |
| 458 | +(define* ival-atan (immutable ival-atan!)) |
472 | 459 |
|
473 | 460 | (define (ival-atan2 y x) |
474 | 461 | (match-define (ival xlo xhi xerr? xerr) x) |
|
503 | 490 | (define* |
504 | 491 | ival-sinh! |
505 | 492 | (overflows-at! (monotonic-mpfr! mpfr-sinh!) (bfneg sinh-overflow-threshold) sinh-overflow-threshold)) |
506 | | -(define (ival-sinh x) |
507 | | - (ival-sinh! (new-ival (bf-precision)) x)) |
| 493 | +(define* ival-sinh (immutable ival-sinh!)) |
508 | 494 |
|
509 | | -(define* ival-cosh! |
510 | | - (compose (overflows-at! (monotonic-mpfr! mpfr-cosh!) |
511 | | - (bfneg acosh-overflow-threshold) |
512 | | - acosh-overflow-threshold) |
513 | | - ival-exact-fabs)) |
514 | | -(define (ival-cosh x) |
515 | | - (ival-cosh! (new-ival (bf-precision)) x)) |
| 495 | +(define (ival-cosh! out x) |
| 496 | + (define absx (ival-exact-fabs x)) |
| 497 | + ((overflows-at! (monotonic-mpfr! mpfr-cosh!) |
| 498 | + (bfneg acosh-overflow-threshold) |
| 499 | + acosh-overflow-threshold) |
| 500 | + out |
| 501 | + absx)) |
| 502 | +(define* ival-cosh (immutable ival-cosh!)) |
516 | 503 |
|
517 | 504 | (define* ival-tanh! (monotonic-mpfr! mpfr-tanh!)) |
518 | | -(define (ival-tanh x) |
519 | | - (ival-tanh! (new-ival (bf-precision)) x)) |
| 505 | +(define* ival-tanh (immutable ival-tanh!)) |
520 | 506 |
|
521 | 507 | (define* ival-asinh! (monotonic-mpfr! mpfr-asinh!)) |
522 | | -(define (ival-asinh x) |
523 | | - (ival-asinh! (new-ival (bf-precision)) x)) |
| 508 | +(define* ival-asinh (immutable ival-asinh!)) |
524 | 509 |
|
525 | 510 | (define* ival-acosh! (clamp! (monotonic-mpfr! mpfr-acosh!) 1.bf +inf.bf)) |
526 | | -(define (ival-acosh x) |
527 | | - (ival-acosh! (new-ival (bf-precision)) x)) |
| 511 | +(define* ival-acosh (immutable ival-acosh!)) |
528 | 512 | (define* ival-atanh! (clamp-strict! (monotonic-mpfr! mpfr-atanh!) -1.bf 1.bf)) |
529 | | -(define (ival-atanh x) |
530 | | - (ival-atanh! (new-ival (bf-precision)) x)) |
| 513 | +(define* ival-atanh (immutable ival-atanh!)) |
531 | 514 |
|
532 | 515 | (define* ival-erf! (monotonic-mpfr! mpfr-erf!)) |
533 | | -(define (ival-erf x) |
534 | | - (ival-erf! (new-ival (bf-precision)) x)) |
| 516 | +(define* ival-erf (immutable ival-erf!)) |
535 | 517 |
|
536 | 518 | (define* ival-erfc! (comonotonic-mpfr! mpfr-erfc!)) |
537 | | -(define (ival-erfc x) |
538 | | - (ival-erfc! (new-ival (bf-precision)) x)) |
| 519 | +(define* ival-erfc (immutable ival-erfc!)) |
539 | 520 |
|
540 | 521 | (define (ival-cmp x y) |
541 | 522 | (define can-< (epfn bflt? (ival-lo x) (ival-hi y))) |
|
0 commit comments