shouldn’t produce a number less than 1.0, but that’s what happens if you use inexact numbers that are implemented as floating-point numbers. Change #lang algebra to treat all numbers as exact numbers, even when the number has a decimal point.Examples:
> (exact? 1.0)
> (exact? 1)
> (define (one-as-input) (open-input-string "1.0")) > (read-syntax 'one (one-as-input))
> (parameterize ([read-decimal-as-inexact #f]) (read-syntax 'one (one-as-input)))
> (define (read-it) (read-syntax 'one (one-as-input))) > (read-it)
> (parameterize ([read-decimal-as-inexact #f]) (read-it))
Exercise 18. Let’s go the other way, and suppose that you want all numbers in #lang algebra to be floating-point numbers, because you don’t want to accidentally perform expensive exact calculations.
There’s no parameter that makes read-syntax treat all numbers as inexact. Fortunately, there’s no loss of information when read-syntax produces an exact number and you want an inexact number; you can convert using exact->inexact.
Exercise 19. Let’s allow the user of #lang algebra pick when to use exact numbers and when to use inexact numbers:
Expression = ... | (exactly Expression) | (inexactly Expression) | (/ Expression Expression)In (exactly Expression), any number that appears literally in Expression should e an exact number, unless it’s in a nested (inexactly Expression), and vice versa.We’re also adding a division operator. That way, an expression can fail at run time. Make sure thatfails with a divide-by-zero error (as happens with exact numbers), as opposed to producing +inf.0 (as happens with inexact numbers). Also make sure that DrRacket highlights specifically the (/ 1.0 0.0) in pink as the source location of the error.
Please fill out today’s post-day survey.