Search code examples
mit-scheme

How does one input positive and negative infinities into MIT Scheme?


Section 4.7.2 of the MIT/GNU Scheme Reference Manual states that

The IEEE floating-point number specification supports three special ‘numbers’: positive infinity (+inf), negative infinity (-inf), and not-a-number (NaN).

These constants, in addition to being well-defined IEEE floating-point values, are also useful for range arithmetic. However, I’m unable to use them in my programs:

1 ]=> +inf

;Unbound variable: +inf

Generating these values isn’t easy, either: expressions which seem like they ought to evaluate to floating-point infinities simply don’t:

1 ]=> (flo:/ 1. 0.)

;Floating-point division by zero

How can I input or generate infinite floating-point constants in MIT Scheme?


Solution

  • tests/runtime/test-arith.scm suggests using flo:with-exceptions-untrapped:

    ;;; XXX The nonsense about IDENTITY-PROCEDURE here serves to fake
    ;;; out bogus constant-folding which needs to be fixed in SF (and
    ;;; probably LIAR too).
    
    (define (zero)
      (identity-procedure 0.))
    
    (define (nan)
      (flo:with-exceptions-untrapped (flo:exception:invalid-operation)
        (lambda ()
          (flo:/ (zero) (zero)))))
    
    (define (inf+)
      (flo:with-exceptions-untrapped (flo:exception:divide-by-zero)
        (lambda ()
          (flo:/ +1. (zero)))))
    
    (define (inf-)
      (flo:with-exceptions-untrapped (flo:exception:divide-by-zero)
        (lambda ()
          (flo:/ -1. (zero)))))
    

    The results display as #[NaN], #[+inf], #[-inf] but cannot be input that way.