functional-programmingschemelispracketinexact-arithmetic

Why is (max (sin (/ 5 3)) (sqr 12)) in Scheme/Dr. Racket (Beginning Student Language) evaluated as inexact?


I was reading How to Design Programs and in the Exercise 2.1.1, I am to:

Find out whether DrScheme has operations for squaring a number; for computing the sine of an angle; and for determining the maximum of two numbers. -- sec. 2.2, ex 2.1.1

My code is:

(sin (/ 5 3))
(sqr 12)
(max (sin (/ 5 3)) (sqr 12))

And the result when I ran the program is:

#i0.9954079577517649
144
#i144.0

I am confused at the last expression where it get's the maximum of #i0.9954079577517649 and 144. Shouldn't the answer be 144 or 144.0 instead of #i144.0?


Solution

  • Scheme will give you an inexact number if either of the inputs to max (or other operations for that matter) are inexact.

    The standards document, R5RS, has this to say in section 6.2.5 Numerical operations when discussing min and max:

    Note: If any argument is inexact, then the result will also be inexact inexact (unless the procedure can prove that the inaccuracy is not large enough to affect the result, which is possible only in unusual implementations).

    Now there's possibly a case to be made that 144 is very much greater than 0.9954079577517649 but it would depend of the level of inaccuracy in that latter number. If the inaccuracy is on the order of a billion percent, it may well be much greater than 144 :-)

    Section 6.2.2 Exactness of that standard also has this:

    Scheme numbers are either exact or inexact. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations.

    A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations.

    Thus inexactness is a contagious property of a number.

    The 0.99-ish number is inexact because sine and many other trigonometric functions are inherently inexact operations. The result of the max is inexact because it has inexact inputs.