Previous: , Up: baselib math   [Index]


4.6.5 Numerical Input and Output

Procedure: number->string z
Procedure: number->string z radix
Procedure: number->string z radix precision

The number->string procedure takes a number object and a radix and returns as a string an external representation of the given number object in the given radix such that:

(let ((number z)
      (radix  radix))
  (eqv?
    (string->number (number->string number radix) radix)
    number))

is true. If no possible result makes this expression true, an exception with condition type &implementation-restriction is raised.

radix must be an exact integer object, either 2, 8, 10, or 16. If omitted, radix defaults to 10. If a precision is specified, then z must be an inexact complex number object, precision must be an exact positive integer object, and radix must be 10.

NOTE The error case can occur only when z is not a complex number object or is a complex number object with a non–rational real or imaginary part.

If a precision is specified, then the representations of the inexact real components of the result, unless they are infinite or NaN, specify an explicit ?mantissa-width p, and p is the least p >= precision for which the above expression is true.

If z is inexact, the radix is 10, and the above expression and condition can be satisfied by a result that contains a decimal point, then the result contains a decimal point and is expressed using the minimum number of digits (exclusive of exponent, trailing zeroes, and mantissa width) needed to make the above expression and condition true; otherwise the format of the result is unspecified.

The result returned by number->string never contains an explicit radix prefix.

Procedure: string->number string
Procedure: string->number string radix

Return a number object with maximally precise representation expressed by the given string.

radix must be an exact integer object, either 2, 8, 10, or 16. If supplied, radix is a default radix that may be overridden by an explicit radix prefix in string (e.g. #o177). If radix is not supplied, then the default radix is 10.

If string is not a syntactically valid notation for a number object or a notation for a rational number object with a zero denominator, then string->number returns #f.

(string->number "100")          ⇒ 100
(string->number "100" 16)       ⇒ 256
(string->number "1e2")          ⇒ 100.0
(string->number "0/0")          ⇒ #f
(string->number "+inf.0")       ⇒ +inf.0
(string->number "-inf.0")       ⇒ -inf.0
(string->number "+nan.0")       ⇒ +nan.0

NOTE The string->number procedure always returns a number object or #f; it never raises an exception.


Previous: , Up: baselib math   [Index]