Node: Numbers, Next: , Previous: Equivalence predicates, Up: Standard Procedures



Numbers

R5RS description of number is quite long and will not be given here. STKLOS support the full number tower as described in R5RS ; see this document for a complete description.

number? obj R5RS
complex? obj R5RS
real? obj R5RS
rational? obj R5RS
integer? obj R5RS
These numerical type predicates can be applied to any kind of argument, including non-numbers. They return #t if the object is of the named type, and otherwise they return #f. In general, if a type predicate is true of a number then all higher type predicates are also true of that number. Consequently, if a type predicate is false of a number, then all lower type predicates are also false of that number.

If z is an inexact complex number, then (real? z) is true if and only if (zero? (imag-part z)) is true. If x is an inexact real number, then (integer? x) is true if and only if (= x (round x)). The (%bignum? x) is true iff x is represented as a bignum in memory.

            (complex? 3+4i)         =>  #t
            (complex? 3)            =>  #t
            (real? 3)               =>  #t
            (real? -2.5+0.0i)       =>  #t
            (real? #e1e10)          =>  #t
            (rational? 6/10)        =>  #t
            (rational? 6/3)         =>  #t
            (integer? 3+0i)         =>  #t
            (integer? 3.0)          =>  #t
            (integer? 8/4)          =>  #t
          

exact? z R5RS
inexact? z R5RS
These numerical predicates provide tests for the exactness of a quantity. For any Scheme number, precisely one of these predicates is true.

= z1 z2 z3 ... R5RS
< x1 x2 x3 ... R5RS
> x1 x2 x3 ... R5RS
<= x1 x2 x3 ... R5RS
>= x1 x2 x3 ... R5RS
These procedures return #t if their arguments are (respectively): equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing.

zero? z R5RS
positive? x R5RS
negative? x R5RS
odd? n R5RS
even? n R5RS
These numerical predicates test a number for a particular property, returning #t or #f.

max x1 x2 ... R5RS
min x1 x2 ... R5RS
These procedures return the maximum or minimum of their arguments.
          (max 3 4)              =>  4    ; exact
          (max 3.9 4)            =>  4.0  ; inexact
          

Note: If any argument is inexact, then the result will also be inexact

+ z1 ... R5RS
* z1 ... R5RS
These procedures return the sum or product of their arguments.
          (+ 3 4)                 =>  7
          (+ 3)                   =>  3
          (+)                     =>  0
          (* 4)                   =>  4
          (*)                     =>  1
          

- z R5RS
- z1 z2 R5RS
/ z R5RS
/ z1 z2 ... R5RS
With two or more arguments, these procedures return the difference or quotient of their arguments, associating to the left. With one argument, however, they return the additive or multiplicative inverse of their argument.
          (- 3 4)                 =>  -1
          (- 3 4 5)               =>  -6
          (- 3)                   =>  -3
          (/ 3 4 5)               =>  3/20
          (/ 3)                   =>  1/3
          

abs x R5RS
Abs returns the absolute value of its argument.
          (abs -7)                =>  7
          

quotient n1 n2 R5RS
remainder n1 n2 R5RS
modulo n1 n2 R5RS
These procedures implement number-theoretic (integer) division. n2 should be non-zero. All three procedures return integers. If n1/n2 is an integer:
          (quotient n1 n2)   => n1/n2
          (remainder n1 n2)  => 0
          (modulo n1 n2)     => 0
          

If n1/n2 is not an integer:

          (quotient n1 n2)   => nq
          (remainder n1 n2)  => nr
          (modulo n1 n2)     => nm
          

where nq is n1/n2 rounded towards zero, 0 < abs(nr) < abs(n2), 0 < abs(nm) < abs(n2), nr and nm differ from n1 by a multiple of n2, nr has the same sign as n1, and nm has the same sign as n2.

From this we can conclude that for integers n1 and n2 with n2 not equal to 0,

           (= n1 (+ (* n2 (quotient n1 n2))
                    (remainder n1 n2)))   =>  #t
          
provided all numbers involved in that computation are exact.
          (modulo 13 4)           =>  1
          (remainder 13 4)        =>  1
          
          (modulo -13 4)          =>  3
          (remainder -13 4)       =>  -1
          
          (modulo 13 -4)          =>  -3
          (remainder 13 -4)       =>  1
          
          (modulo -13 -4)         =>  -1
          (remainder -13 -4)      =>  -1
          
          (remainder -13 -4.0)    =>  -1.0  ; inexact
          

gcd n1 ... R5RS
lcm n1 ... R5RS
These procedures return the greatest common divisor or least common multiple of their arguments. The result is always non-negative.
          (gcd 32 -36)            =>  4
          (gcd)                   =>  0
          (lcm 32 -36)            =>  288
          (lcm 32.0 -36)          =>  288.0  ; inexact
          (lcm)                   =>  1
          

numerator q R5RS
denominator q R5RS
These procedures return the numerator or denominator of their argument; the result is computed as if the argument was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0 is defined to be 1.
          (numerator (/ 6 4))  =>  3
          (denominator (/ 6 4))  =>  2
          (denominator
          (exact->inexact (/ 6 4))) => 2.0
          

floor x R5RS
ceiling x R5RS
truncate x R5RS
round x R5RS
These procedures return integers. Floor returns the largest integer not larger than x. Ceiling returns the smallest integer not smaller than x. Truncate returns the integer closest to x whose absolute value is not larger than the absolute value of x. Round returns the closest integer to x, rounding to even when x is halfway between two integers.

Rationale: Round rounds to even for consistency with the default rounding mode specified by the IEEE floating point standard.

Note: If the argument to one of these procedures is inexact, then the result will also be inexact. If an exact value is needed, the result should be passed to the inexact->exact procedure.

          
          (floor -4.3)          =>  -5.0
          (ceiling -4.3)        =>  -4.0
          (truncate -4.3)       =>  -4.0
          (round -4.3)          =>  -4.0
          
          (floor 3.5)           =>  3.0
          (ceiling 3.5)         =>  4.0
          (truncate 3.5)        =>  3.0
          (round 3.5)           =>  4.0  ; inexact
          
          (round 7/2)           =>  4    ; exact
          (round 7)             =>  7
          

rationalize x y R5RS
Rationalize returns the simplest rational number differing from x by no more than y. A rational number r1 is simpler than another rational number r2 if r1 = p1/q1 and r2 = p2/q2 (in lowest terms) and abs(p1) <= abs(p2) and abs(q1) <= abs(q2). Thus 3/5 is simpler than 4/7. Although not all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of all.
          (rationalize
             (inexact->exact .3) 1/10)  => 1/3    ; exact
          (rationalize .3 1/10)         => #i1/3  ; inexact
          

exp z R5RS
log z R5RS
sin z R5RS
cos z R5RS
tan z R5RS
asin z R5RS
acos z R5RS
atan z R5RS
atan y x R5RS
These procedures compute the usual transcendental functions. Log computes the natural logarithm of z (not the base ten logarithm). Asin, acos, and atan compute arcsine, arccosine, and arctangent, respectively. The two-argument variant of atan computes
          (angle (make-rectangular x y))
          

When it is possible these procedures produce a real result from a real argument.

sqrt z R5RS
Returns the principal square root of z. The result will have either positive real part, or zero real part and non-negative imaginary part.

expt z1 z2 R5RS
Returns z1 raised to the power z2.

Note: Then

          (make-rectangular x1 x2)       => z
          (make-polar x3 x4)             => z
          (real-part z)                  => x1
          (imag-part z)                  => x2
          (magnitude z)                  => abs(x3)
          (angle z)                      => xa
          
where for some integer n.

Note: Magnitude is the same as abs for a real argument.

exact->inexact z R5RS
inexact->exact z R5RS
Exact->inexact returns an inexact representation of z. The value returned is the inexact number that is numerically closest to the argument. Inexact->exact returns an exact representation of z. The value returned is the exact number that is numerically closest to the argument.

number->string z R5RS
number->string z radix R5RS
Radix must be an exact integer, either 2, 8, 10, or 16. If omitted, radix defaults to 10. The procedure number->string takes a number and a radix and returns as a string an external representation of the given number in the given radix such that
          (let ((number number)
                (radix radix))
            (eqv? number
                 (string->number (number->string number radix) radix)))
          
is true. It is an error if no possible result makes this expression true.

If z is inexact, the radix is 10, and the above expression 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 and trailing zeroes) needed to make the above expression true; otherwise the format of the result is unspecified.

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

Note: The error case can occur only when z is not a complex number or is a complex number with a non-rational real or imaginary part.

Rationale: If z is an inexact number represented using flonums, and the radix is 10, then the above expression is normally satisfied by a result containing a decimal point. The unspecified case allows for infinities, NaNs, and non-flonum representations.

string->number string R5RS
string->number string radix R5RS
Returns a number of the maximally precise representation expressed by the given string. Radix must be an exact integer, 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, then string->number returns #f.
          (string->number "100")        =>  100
          (string->number "100" 16)     =>  256
          (string->number "1e2")        =>  100.0
          (string->number "15##")       =>  1500.0
          

bit-and n1 n2 ... STKLOS Procedure
bit-or n1 n2 ... STKLOS Procedure
bit-xor n1 n2 ... STKLOS Procedure
bit-not n STKLOS Procedure
bit-shift n m STKLOS Procedure
These procedures allow the manipulation of integers as bit fields. The integers can be of arbitrary length. Bit-and, bit-or and bit-xor respectively compute the bitwise and, inclusive and exclusive or. bit-not eturns the bitwise not of n. bit-shift returns the bitwise shift of n. The integer n is shifted left by m bits; If m is negative, n is shifted right by -m bits.
          (bit-or 5 3)       => 7
          (bit-xor 5 3)      => 6
          (bit-and 5 3)      => 1
          (bit-not 5)        => -6
          (bit-or 1 2 4 8)   => 15
          (bit-shift 5 3)    => 40
          (bit-shift 5 -1)   => 2
          

random-integer n STKLOS Procedure
Return an integer in the range [0, ..., n[. Subsequent results of this procedure appear to be independent uniformly distributed over the range [0, ..., n[. The argument n must be a positive integer, otherwise an error is signaled. This function is equivalent to the eponym function of SRFI-27 (see SRFI-27 definition for more details)

random-real STKLOS Procedure
Return a real number r such that $0 < r < 1$. Subsequent results of this procedure appear to be independent uniformly distributed. This function is equivalent to the eponym function of SRFI-27 (see SRFI-27 definition for more details)