On this page:
3.2.1 Number Types
number?
complex?
real?
rational?
integer?
exact-integer?
exact-nonnegative-integer?
exact-positive-integer?
inexact-real?
fixnum?
zero?
positive?
negative?
even?
odd?
exact?
inexact?
inexact->exact
exact->inexact
3.2.2 Arithmetic
+
-
*
/
quotient
remainder
quotient/ remainder
modulo
add1
sub1
abs
max
min
gcd
lcm
round
floor
ceiling
truncate
numerator
denominator
rationalize
3.2.3 Number Comparison
=
<
<=
>
>=
3.2.4 Powers and Roots
sqrt
integer-sqrt
integer-sqrt/ remainder
expt
exp
log
3.2.5 Trignometric Functions
sin
cos
tan
asin
acos
atan
3.2.6 Complex Numbers
make-rectangular
make-polar
real-part
imag-part
magnitude
angle
3.2.7 Bitwise Operations
bitwise-ior
bitwise-and
bitwise-xor
bitwise-not
bitwise-bit-set?
arithmetic-shift
integer-length
3.2.8 Random Numbers
random
random-seed
make-pseudo-random-generator
pseudo-random-generator?
current-pseudo-random-generator
pseudo-random-generator->vector
vector->pseudo-random-generator
vector->pseudo-random-generator!
3.2.9 Number–String Conversions
number->string
string->number
real->decimal-string
integer-bytes->integer
integer->integer-bytes
floating-point-bytes->real
real->floating-point-bytes
system-big-endian?
3.2.10 Extra Constants and Functions
pi
sqr
sgn
conjugate
sinh
cosh
Version: 4.1.0.2

3.2 Numbers

Numbers in Guide: PLT Scheme introduces numbers.

All numbers are complex numbers. Some of them are real numbers, and all of the real numbers that can be represented are also rational numbers, except for +inf.0 (positive infinity), -inf.0 (negative infinity), and +nan.0 (not-a-number). Among the rational numbers, some are integers, because round applied to the number produces the same number.

Orthogonal to those categories, each number is also either an exact number or an inexact number. Unless otherwise specified, computations that involve an inexact number produce inexact results. Certain operations on inexact numbers, however, produce an exact number, such as multiplying an inexact number with an exact 0. Some operations, which can produce an irrational number for rational arguments (e.g., sqrt), may produce inexact results even for exact arguments.

In the case of complex numbers, either the real and imaginary parts are both exact or inexact, or the number has an exact zero real part and an inexact imaginary part; a complex number with an exact zero imaginary part is a real number.

Inexact real numbers are implemented as either single- or double-precision IEEE floating-point numbers – the latter by default, and the former only when support for 32-bit inexact numbers is specifically enabled when the run-time system is built, and when computation starts with numerical constants specified as single-precision numbers.

The precision and size of exact numbers is limited only by available memory (and the precision of operations that can produce irrational numbers). In particular, adding, multiplying, subtracting, and dividing exact numbers always produces an exact result.

Inexact numbers can be coerced to exact form, except for the inexact numbers +inf.0, -inf.0, and +nan.0, which have no exact form. Dividing a number by exact zero raises an exception; dividing a non-zero number other than +nan.0 by an inexact zero returns +inf.0 or -inf.0, depending on the sign of the dividend. The +nan.0 value is not = to itself, but +nan.0 is eqv? to itself. Conversely, (= 0.0 -0.0) is #t, but (eqv? 0.0 -0.0) is #f. The datum -nan.0 refers to the same constant as +nan.0.

Calculations with infinites produce results consistent with IEEE double-precision floating point where IEEE specifies the result; in cases where IEEE provides no specification (e.g., (angle +inf.0+inf.0i)), the result corresponds to the limit approaching infinity, or +nan.0 if no such limit exists.

A fixnum is an exact integer whose two’s complement representation fit into 31 bits on a 32-bit platform or 63 bits on a 64-bit platform. Two fixnums that are = are also the same according to eq?. Otherwise, the result of eq? applied to two numbers is undefined.

Two numbers are eqv? when they are both inexact or both exact, and when they are = (except for +nan.0, as noted above). Two numbers are equal? when they are eqv?.

3.2.1 Number Types

(number? v)  boolean?

  v : any/c

Returns #t if v is a number, #f otherwise.

Examples:

  > (number? 1)

  #t

  > (number? 2+3i)

  #t

  > (number? "hello")

  #f

(complex? v)  boolean?

  v : any/c

Returns (number? v), because all numbers are complex numbers.

(real? v)  boolean?

  v : any/c

Returns #t if v is a real number, #f otherwise.

Examples:

  > (real? 1)

  #t

  > (real? +inf.0)

  #t

  > (real? 2+3i)

  #f

  > (real? 2.0+0.0i)

  #f

  > (real? "hello")

  #f

(rational? v)  boolean?

  v : any/c

Returns #t if v is a rational number, #f otherwise.

Examples:

  > (rational? 1)

  #t

  > (rational? +inf.0)

  #f

  > (real? "hello")

  #f

(integer? v)  boolean?

  v : any/c

Returns #t if v is a number that is an integer, #f otherwise.

Examples:

  > (integer? 1)

  #t

  > (integer? 2.3)

  #f

  > (integer? 4.0)

  #t

  > (integer? +inf.0)

  #f

  > (integer? 2+3i)

  #f

  > (integer? "hello")

  #f

(exact-integer? v)  boolean?

  v : any/c

Returns (and (integer? v) (exact? v)).

Examples:

  > (exact-integer? 1)

  #t

  > (exact-integer? 4.0)

  #f

(exact-nonnegative-integer? v)  boolean?

  v : any/c

Returns (and (exact-integer? v) (not (negative? v))).

Examples:

  > (exact-nonnegative-integer? 0)

  #t

  > (exact-nonnegative-integer? -1)

  #f

(exact-positive-integer? v)  boolean?

  v : any/c

Returns (and (exact-integer? v) (positive? v)).

Examples:

  > (exact-positive-integer? 1)

  #t

  > (exact-positive-integer? 0)

  #f

(inexact-real? v)  boolean?

  v : any/c

Returns (and (real? v) (inexact? v)).

(fixnum? v)  boolean?

  v : any/c

Return #t if v is a fixnum, #f otherwise.

(zero? z)  boolean?

  z : number?

Returns (= 0 z).

Examples:

  > (zero? 0)

  #t

  > (zero? -0.0)

  #t

(positive? x)  boolean?

  x : real?

Returns (> x 0).

Examples:

  > (positive? 10)

  #t

  > (positive? -10)

  #f

  > (positive? 0.0)

  #f

(negative? x)  boolean?

  x : real?

Returns (< x 0).

Examples:

  > (negative? 10)

  #f

  > (negative? -10)

  #t

  > (negative? -0.0)

  #f

(even? n)  boolean?

  n : integer?

Returns (zero? (modulo n 2)).

Examples:

  > (even? 10.0)

  #t

  > (even? 11)

  #f

  > (even? +inf.0)

  even?: expects argument of type <integer>; given +inf.0

(odd? n)  boolean?

  n : integer?

Returns (not (even? n)).

Examples:

  > (odd? 10.0)

  #f

  > (odd? 11)

  #t

  > (odd? +inf.0)

  odd?: expects argument of type <integer>; given +inf.0

(exact? z)  boolean?

  z : number?

Returns #t if z is an exact number, #f otherwise.

Examples:

  > (exact? 1)

  #t

  > (exact? 1.0)

  #f

(inexact? z)  boolean?

  z : number?

Returns #t if z is an inexact number, #f otherwise.

Examples:

  > (inexact? 1)

  #f

  > (inexact? 1.0)

  #t

(inexact->exact z)  exact?

  z : number?

Coerces z to an exact number. If z is already exact, it is returned. If z is +inf.0, -inf.0, or +nan.0, then the exn:fail:contract exception is raised.

Examples:

  > (inexact->exact 1)

  1

  > (inexact->exact 1.0)

  1

(exact->inexact z)  inexact?

  z : number?

Coerces z to an inexact number. If z is already inexact, it is returned.

Examples:

  > (exact->inexact 1)

  1.0

  > (exact->inexact 1.0)

  1.0

3.2.2 Arithmetic

(+ z ...)  number?

  z : number?

Returns the sum of the zs, adding pairwise from left to right. If no arguments are provided, the result is 0.

Examples:

  > (+ 1 2)

  3

  > (+ 1.0 2+3i 5)

  8.0+3.0i

  > (+)

  0

(- z)  number?

  z : number?

(- z w ...+)  number?

  z : number?

  w : number?

When no ws are supplied, returns (- 0 z). Otherwise, returns the subtraction of the ws from z working pairwise from left to right.

Examples:

  > (- 5 3.0)

  2.0

  > (- 1)

  -1

  > (- 2+7i 1 3)

  -2+7i

(* z ...)  number?

  z : number?

Returns the product of the zs, multiplying pairwise from left to right. If no arguments are provided, the result is 1.

Examples:

  > (* 2 3)

  6

  > (* 8.0 9)

  72.0

  > (* 1+2i 3+4i)

  -5+10i

(/ z)  number?

  z : number?

(/ z w ...+)  number?

  z : number?

  w : number?

When no ws are supplied, returns (/ 1 z). Otherwise, returns the division z by the var[w]s working pairwise from left to right.

Examples:

  > (/ 3 4)

  3/4

  > (/ 81 3 3)

  9

  > (/ 10.0)

  0.1

  > (/ 1+2i 3+4i)

  11/25+2/25i

(quotient n m)  integer?

  n : integer?

  m : integer?

Returns (truncate (/ n m)).

Examples:

  > (quotient 10 3)

  3

  > (quotient -10.0 3)

  -3.0

  > (quotient +inf.0 3)

  quotient: expects type <integer> as 1st argument, given:

  +inf.0; other arguments were: 3

(remainder n m)  integer?

  n : integer?

  m : integer?

Returns q with the same sign as n such that

Examples:

  > (remainder 10 3)

  1

  > (remainder -10.0 3)

  -1.0

  > (remainder 10.0 -3)

  1.0

  > (remainder -10 -3)

  -1

  > (remainder +inf.0 3)

  remainder: expects type <integer> as 1st argument, given:

  +inf.0; other arguments were: 3

(quotient/remainder n m)

 

 

number?

 

number?

  n : integer?

  m : integer?

Returns (values (quotient n m) (remainder n m)), but the combination is computed more efficiently than separate calls to quotient and remainder.

Examples:

  > (quotient/remainder 10 3)

  3

  1

(modulo n m)  number?

  n : integer?

  m : integer?

Returns q with the same sign as m where

Examples:

  > (modulo 10 3)

  1

  > (modulo -10.0 3)

  2.0

  > (modulo 10.0 -3)

  -2.0

  > (modulo -10 -3)

  -1

  > (modulo +inf.0 3)

  modulo: expects type <integer> as 1st argument, given:

  +inf.0; other arguments were: 3

(add1 z)  number?

  z : number?

Returns (+ z 1).

(sub1 z)  number?

  z : number?

Returns (- z 1).

(abs x)  number?

  x : real?

Returns the absolute value of x.

</

Examples:

  > (abs 1.0)

  1.0

  > (abs -1)

  1