Next: , Previous: , Up: Numbers   [Contents][Index]


6.6.2.5 Exact and Inexact Numbers

R5RS requires that, with few exceptions, a calculation involving inexact numbers always produces an inexact result. To meet this requirement, Guile distinguishes between an exact integer value such as ‘5’ and the corresponding inexact integer value which, to the limited precision available, has no fractional part, and is printed as ‘5.0’. Guile will only convert the latter value to the former when forced to do so by an invocation of the inexact->exact procedure.

The only exception to the above requirement is when the values of the inexact numbers do not affect the result. For example (expt n 0) is ‘1’ for any value of n, therefore (expt 5.0 0) is permitted to return an exact ‘1’.

Scheme Procedure: exact? z
C Function: scm_exact_p (z)

Return #t if the number z is exact, #f otherwise.

(exact? 2)
⇒ #t

(exact? 0.5)
⇒ #f

(exact? (/ 2))
⇒ #t
C Function: int scm_is_exact (SCM z)

Return a 1 if the number z is exact, and 0 otherwise. This is equivalent to scm_is_true (scm_exact_p (z)).

An alternate approch to testing the exactness of a number is to use scm_is_signed_integer or scm_is_unsigned_integer.

Scheme Procedure: inexact? z
C Function: scm_inexact_p (z)

Return #t if the number z is inexact, #f else.

C Function: int scm_is_inexact (SCM z)

Return a 1 if the number z is inexact, and 0 otherwise. This is equivalent to scm_is_true (scm_inexact_p (z)).

Scheme Procedure: inexact->exact z
C Function: scm_inexact_to_exact (z)

Return an exact number that is numerically closest to z, when there is one. For inexact rationals, Guile returns the exact rational that is numerically equal to the inexact rational. Inexact complex numbers with a non-zero imaginary part can not be made exact.

(inexact->exact 0.5)
⇒ 1/2

The following happens because 12/10 is not exactly representable as a double (on most platforms). However, when reading a decimal number that has been marked exact with the “#e” prefix, Guile is able to represent it correctly.

(inexact->exact 1.2)  
⇒ 5404319552844595/4503599627370496

#e1.2
⇒ 6/5
Scheme Procedure: exact->inexact z
C Function: scm_exact_to_inexact (z)

Convert the number z to its inexact representation.


Next: , Previous: , Up: Numbers   [Contents][Index]