How does the Scheme procedure inexact->exact
, described in SICP, operate?

- 11,661
- 4
- 46
- 85

- 11
- 1
- 2
2 Answers
The Scheme standard only gives some general constraints on how exactness/inexactness is recorded, but most Scheme implementations, up to standard R5RS, operate as follows (MIT Scheme, which is SICP's "mother tongue", also works this way):
- The type information for each cell that contains data of a numeric type says whether the data is exact or inexact.
- Arithmetic operations on the data record derive the exactness of the result from the exactness of the inputs, where generally inexactness is infectious: if any of the operands is inexact, the result probably will be so too. Note, though, Scheme implementations are allowed to infer exactness in special cases, say if you multiply inexact 4.3 by exact 0, you can know the result is 0 exactly.
- The special operations inexact->exact and exact->inexact are casts on the numeric types, ensuring that the resulting type is exact or inexact respectively.
Some points: first, different scheme standards vary in when operators give exactness or not; the standards underdetermine what happens. For example, several Scheme implementations have representations for exact rationals, allowing (/ 1 3)
to be represented exactly, where a Scheme implementation with only floats must represent this inexactly.
Second, R6RS has a different notion of contagion from that of SICP and earlier standards, because the older criterion is, frankly, broken.

- 11,661
- 4
- 46
- 85
Exactness is simply a property of a number: it doesn't change the value of the number itself. So, for an implementation that uses a flag to indicate exactness, inexact->exact
simply sets the exactness flag on that number.

- 219,335
- 46
- 382
- 435
-
This is not correct -- most scheme implementations implement inexact numbers with IEEE 754 floating point, and exact numbers using arbitrary precision rationals. Conversion between these two is not just setting a flag. – Sam Tobin-Hochstadt Dec 29 '10 at 23:14
-
@Sam: That is an implementation detail, which is not required by Scheme per se. An implementation is free to use IEEE754 floating point datatypes to represent exact numbers (which have to be upgraded to other datatypes, like rationals, if further results don't fit, of course), or to use rationals to represent inexact, if it wishes. – C. K. Young Dec 29 '10 at 23:54