Next: Characters Up: Basic Data Extensions Previous: Booleans

# Numbers

A number in MzScheme is one of the following:

- a fixnum exact integer (30 bits plus a sign bit)
- a bignum exact integer (cannot be represented in a fixnum)
- a fraction exact rational (represented by two exact integers)
- a flonum inexact rational (double-precision floating-point number)
- a complex number; 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 inexact zero imaginary part is a real number

MzScheme extends the number syntax of R5RS in two ways:

- All input radixes (`#b`, `#o`, `#d`, and `#x`) allow ``decimal'' numbers that contain a period or exponent marker. For example, `#b1.1` is equivalent to `1.5`. In hexadecimal numbers, `e` always stands for a hexadecimal digit, not an exponent marker.
- The following are inexact numerical constants: `+inf.0` (infinity), `-inf.0` (negative infinity), `+nan.0` (not a number), and `-nan.0` (same as `+nan.0`). These names can also be used within complex constants, as in `-inf.0+inf.0i`.

The special inexact numbers `+inf.0`, `-inf.0`, and `+nan.0` have no exact form. Dividing by an inexact zero returns `+inf.0` or `-inf.0`, depending on the sign of the dividend. The infinities are integers, and they answer `#t` for both `even?` and `odd?`. The `+nan.0` value is not an integer and is not `=` to itself, but `+nan.0` is `eqv?` to itself. Similarly, `(= 0.0 -0.0)` is `#t`, but `(eqv? 0.0 -0.0)` is `#f`.

All multi-argument arithmetic procedures operate pairwise on arguments from left to right.

The `string->number` procedure works on all number representations and exact integer radix values in the range `2` to `16` (inclusive). The `number->string` procedure accepts all number types and the radix values `2`, `8`, `10`, and `16`; however, if an inexact number is provided with a radix other than `10`, the `exn:application:mismatch` exception is raised.

The `add1` and `sub1` procedures work on any number:

- `(add1 z)` returns .
- `(sub1 z)` returns .

The following procedures work on exact integers in their (semi-infinite) two's complement representation:

- `(bitwise-ior n )` returns the bitwise ``inclusive or'' of the ns.
- `(bitwise-and n )` returns the bitwise ``and'' of the ns.
- `(bitwise-xor n )` returns the bitwise ``exclusive or'' of the ns.
- `(bitwise-not n)` returns the bitwise ``not'' of n.
- `(arithmetic-shift n m)` returns the bitwise ``shift'' of n. The integer n is shifted left by m bits; i.e., m new zeros are introduced as rightmost digits. If m is negative, n is shifted right by -m bits; i.e., the rightmost -m digits are dropped.

The `random` procedure generates pseudo-random integers:

- `(random k)` returns a random exact integer in the range `0` to where k is an exact integer between 1 and , inclusive. The number is provided by the current pseudo-random number generator, which maintains an internal state for generating numbers.
- `(random-seed k)` seeds the current pseudo-random number generator with k, an exact integer between 0 and , inclusive. Seeding a generator sets its internal state deterministically; seeding a generator with a particular number forces it to produce a sequence of pseudo-random numbers that is the same across runs and across platforms.
- `(current-pseudo-random-generator)` returns the current pseudo-random number generator, and `(current-pseudo-random-generator generator)` sets the current generator to generator. See also section 9.4.1.13.
- `(make-pseudo-random-generator)` returns a new pseudo-random number generator. The new generator is seeded with a number derived from `(`current-milliseconds`)`.
- `(pseudo-random-generator? v)` returns `#t` if v is a pseudo-random number generator, `#f` otherwise.

Next: Characters Up: Basic Data Extensions Previous: Booleans