[racket] Multiplying by 0

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Mon Feb 14 15:59:04 EST 2011

>>> (define (min n1 n2)
>>>  (cond [(<= n1 n2) n1]
>>>        [else n2]))
>> 
>> What's wrong with this is that, mathematically, since 1e100 is inexact, we're not CERTAIN it's>= 0, so the "proper" answer to (<= n1 n2) is not true but rather almost-certainly-true.  (An "inexact Boolean", if you will....)
>> 
>> When you define the function as above, the "<=" takes its best guess as to which number is really smaller and pretends that the answer is certain.
> 
> Then what is the correct definition?

I guess if you wanted to be really pedantic (and get the same answer as the built-in min produces), you could say

(define (min n1 n2)
   (let ((naive-min (if (<= n1 n2) n1 n2)))
      (if (and (exact? n1) (exact? n2))
          naive-min
          (exact->inexact naive-min))))




Stephen Bloch
sbloch at adelphi.edu




Posted on the users mailing list.