[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.2.5 Numerical conversions

Function: make-rectangular x1 x2
Function: make-polar x1 x2
[R5RS] Creates a complex number from two real numbers, x1 and x2. make-rectangular returns x1 + ix2. make-polar returns x1e^(ix2).

Function: real-part z
Function: imag-part z
Function: magnitude z
Function: angle z
[R5RS] Decompose a complex number z and returns a real number. real-part and imag-part return z's real and imaginary part, respectively. magnitude and angle return z's magnitude and angle, respectively.

Function: decode-float x
Given floating-point number (inexact real number), returns a vector of three exact integers, #(m, e, sign), where
 
  x = (* sign m (expt 2.0 e))
  sign is either 1, 0 or -1.
The API is taken from ChezScheme.
 
(decode-float 3.1415926)
 => #(7074237631354954 -51 1)
(* 7074237631354954 (expt 2.0 -51))
 => 3.1415926

Function: fmod x y
Function: modf x
Function: frexp x
Function: ldexp x n
[POSIX] These procedures can be used to compose and decompose floating point numbers. Fmod computes the remainder of dividing x by y, that is, it returns x-n*y where n is the quotient of x/y rounded towards zero to an integer. Modf returns two values; a fractional part of x and an integral part of x. Frexp returns two values, fraction and exponent of x, where x = fraction * 2^exponent, and 0 <= fraction <= 0.5. Ldexp is a reverse operation of frexp; it returns a real number x * 2^n.
 
(fmod 32.1 10.0)  => 2.1
(fmod 1.5 1.4)    => 0.1
(modf 12.5)       => 0.5 and 12.0
(frexp 3.14)      => 0.785 and 2
(ldexp 0.785 2)   => 3.14

Function: exact->inexact z
Function: inexact->exact z
[R5RS] Converts an exact number to an inexact number, or vice versa.

Since Gauche doesn't support general rational numbers, the result of inexact->exact is always a whole number. If the passed number is not an integer, the fraction part is rounded. Note that it is an implementation dependent behavior. If you want to get a closest exact integer of the given inexact real number, it's better to use round explicitly before inexact->exact.

If you pass an inexact number to exact->inexact or an exact number to inexact->exact, Gauche returns the argument as is, instead of reporting an error. This is also an implementation dependent behavior and you shouldn't count on that.

Function: number->string z &optional radix use-upper?
Function: string->number string &optional radix
[R5RS+] These procedurs convert a number and its string representation in radix radix system. radix must be between 2 and 36 inclusive. If radix is omitted, 10 is assumed.

Number->string takes a number z and returns a string. If z is not an exact integer, radix must be 10. For the numbers with radix more than 10, lower case alphabet character is used for digits, unless the optional argument use-upper? is true, in that case upper case characters are used. The argument use-upper? is Gauche's extension.

String->number takes a string string and parses it as a number in radix radix system. If the number looks like non-exact number, only radix 10 is allowed. If the given string can't be a number, #f is returned.

Generic Function: x->number obj
Generic Function: x->integer obj
Generic coercion functions. Returns `natural' interpretation of obj as a number or an exact integer, respectively. The default methods are defined for numbers and strings; a string is interpreted by string->number, and if the string can't be interpreted as a number, 0 is returned. Other obj is simply converted to 0. If obj is naturally interpreted as a number that is not an exact integer, x->integer uses round and inexact->exact to obtain an integer.

Other class may provide a method to customize the behavior.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html