4.3.2 Generic Numerics
The Racket Reference
Language Model
Notation for Documentation
Syntactic Forms
Datatypes
Structures
Classes and Objects
Units
Contracts
Pattern Matching
10
Control Flow
11
Concurrency and Parallelism
12
Macros
13
Input and Output
14
Reflection and Security
15
Operating System
16
Memory Management
17
Unsafe Operations
18
Running Racket
Bibliography
Index
Datatypes
4.1
Equality
4.2
Booleans
4.3
Numbers
4.4
Strings
4.5
Byte Strings
4.6
Characters
4.7
Symbols
4.8
Regular Expressions
4.9
Keywords
4.10
Pairs and Lists
4.11
Mutable Pairs and Lists
4.12
Vectors
4.13
Stencil Vectors
4.14
Boxes
4.15
Hash Tables
4.16
Treelists
4.17
Sequences and Streams
4.18
Dictionaries
4.19
Sets
4.20
Procedures
4.21
Void
4.22
Undefined
4.3
Numbers
4.3.1
Number Types
4.3.2
Generic Numerics
4.3.3
Flonums
4.3.4
Fixnums
4.3.5
Extflonums
4.3.2
Generic Numerics
4.3.2.1
Arithmetic
4.3.2.2
Number Comparison
4.3.2.3
Powers and Roots
4.3.2.4
Trigonometric Functions
4.3.2.5
Complex Numbers
4.3.2.6
Bitwise Operations
4.3.2.7
Random Numbers
4.3.2.8
Other Randomness Utilities
4.3.2.9
Number–String Conversions
4.3.2.10
Extra Constants and Functions
On this page:
4.3.2.1
Arithmetic
quotient
remainder
quotient/
remainder
modulo
add1
sub1
abs
max
min
gcd
lcm
round
floor
ceiling
truncate
numerator
denominator
rationalize
4.3.2.2
Number Comparison
<=
>=
4.3.2.3
Powers and Roots
sqrt
integer-
sqrt
integer-
sqrt/
remainder
expt
exp
log
4.3.2.4
Trigonometric Functions
sin
cos
tan
asin
acos
atan
4.3.2.5
Complex Numbers
make-
rectangular
make-
polar
real-
part
imag-
part
magnitude
angle
4.3.2.6
Bitwise Operations
bitwise-
ior
bitwise-
and
bitwise-
xor
bitwise-
not
bitwise-
bit-
set?
bitwise-
first-
bit-
set
bitwise-
bit-
field
arithmetic-
shift
integer-
length
4.3.2.7
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!
pseudo-
random-
generator-
vector?
4.3.2.8
Other Randomness Utilities
crypto-
random-
bytes
random-
ref
random-
sample
4.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?
4.3.2.10
Extra Constants and Functions
pi
pi.f
degrees-
>radians
radians-
>degrees
sqr
sgn
conjugate
sinh
cosh
tanh
exact-
round
exact-
floor
exact-
ceiling
exact-
truncate
order-
of-
magnitude
nan?
infinite?
positive-
integer?
negative-
integer?
nonpositive-
integer?
nonnegative-
integer?
natural?
Racket
top
contents
← prev
up
next →
4.3.2
Generic Numerics
Most Racket numeric operations work on any kind of number.
4.3.2.1
Arithmetic
procedure
...
number?
number?
Returns the sum of the
s, adding pairwise from left to
right. If no arguments are provided, the result is
Examples:
1.0
2+3i
8.0+3.0i
procedure
number?
number?
...+
number?
number?
number?
When no
s are supplied, returns
Otherwise, returns the subtraction of the
s from
working pairwise from left to right.
Examples:
3.0
2.0
-1
2+7i
-2+7i
procedure
...
number?
number?
Returns the product of the
s, multiplying pairwise from left
to right. If no arguments are provided, the result is
. Multiplying any number by exact
produces exact
Examples:
8.0
72.0
1+2i
3+4i
-5+10i
procedure
number?
number?
...+
number?
number?
number?
When no
s are supplied, returns
Otherwise, returns the division of
by the
s working
pairwise from left to right.
If
is exact
and no
is exact
, then the result is exact
. If any
is
exact
, the
exn:fail:contract:divide-by-zero
exception is raised.
Examples:
3/4
81
10.0
0.1
1+2i
3+4i
11/25+2/25i
procedure
quotient
integer?
integer?
integer?
Returns
truncate
Examples:
quotient
10
quotient
-1
0.0
-3.0
quotient
+inf.0
quotient: contract violation
expected: integer?
given: +inf.0
procedure
remainder
integer?
integer?
integer?
Returns
with the same sign as
such that
abs
is between
(inclusive) and
abs
(exclusive), and
quotient
equals
If
is exact
, the
exn:fail:contract:divide-by-zero
exception is raised.
Examples:
remainder
10
remainder
-1
0.0
-1.0
remainder
10.0
-3
1.0
remainder
-1
-3
-1
remainder
+inf.0
remainder: contract violation
expected: integer?
given: +inf.0
procedure
quotient/remainder
integer?
integer?
integer?
integer?
Returns
values
quotient
remainder
, but the
combination may be computed more efficiently than separate calls to
quotient
and
remainder
Example:
quotient/remainder
10
procedure
modulo
integer?
integer?
integer?
Returns
with the same sign as
where
abs
is between
(inclusive) and
abs
(exclusive), and
the difference between
and
quotient
is a multiple of
If
is exact
, the
exn:fail:contract:divide-by-zero
exception is raised.
Examples:
modulo
10
modulo
-1
0.0
2.0
modulo
10.0
-3
-2.0
modulo
-1
-3
-1
modulo
+inf.0
modulo: contract violation
expected: integer?
given: +inf.0
procedure
add1
number?
number?
Returns
procedure
sub1
number?
number?
Returns
procedure
abs
number?
real?
Returns the absolute value of
Examples:
abs
1.0
1.0
abs
-1
procedure
max
...+
real?
real?
Returns the largest of the
s, or
+nan.0
if any
is
+nan.0
. If any
is inexact, the
result is coerced to inexact. See also
argmax
Examples:
max
max
2.0
3.0
procedure
min
...+
real?
real?
Returns the smallest of the
s, or
+nan.0
if any
is
+nan.0
. If any
is inexact, the
result is coerced to inexact. See also
argmin
Examples:
min
min
2.0
1.0
procedure
gcd
...
rational?
rational?
Returns the
greatest common divisor (a non-negative
number) of the
s; for non-integer
s, the result
is the
gcd
of the numerators divided
by the
lcm
of the denominators.
If no arguments are provided, the result
is
. If all arguments are zero, the result is zero.
Examples:
gcd
10
10
gcd
12
81.0
3.0
gcd
1/2
1/3
1/6
procedure
lcm
...
or/c
rational?
+inf.0
rational?
Returns the
least common multiple (a non-negative number)
of the
s. For two non-integer
s, the result is
the absolute value of the product divided by the
gcd
. If no arguments are provided, the result is
. If any argument is zero, the result is zero; furthermore,
if any argument is exact
, the result is exact
Examples:
lcm
10
10
lcm
4.0
12.0
lcm
1/2
2/3
procedure
round
or/c
integer?
+inf.0
-i
nf.0
+nan.0
real?
Returns the integer closest to
, resolving ties in favor of
an even number, but
+inf.0
-i
nf.0
, and
+nan.0
round to themselves.
Examples:
round
17/4
round
-1
7/4
-4
round
2.5
2.0
round
-2
.5
-2.0
round
+inf.0
+inf.0
procedure
floor
or/c
integer?
+inf.0
-i
nf.0
+nan.0
real?
Returns the largest integer that is no more than
, but
+inf.0
-i
nf.0
, and
+nan.0
floor to
themselves.
Examples:
floor
17/4
floor
-1
7/4
-5
floor
2.5
2.0
floor
-2
.5
-3.0
floor
+inf.0
+inf.0
procedure
ceiling
or/c
integer?
+inf.0
-i
nf.0
+nan.0
real?
Returns the smallest integer that is at least as large as
but
+inf.0
-i
nf.0
, and
+nan.0
ceiling to
themselves.
Examples:
ceiling
17/4
ceiling
-1
7/4
-4
ceiling
2.5
3.0
ceiling
-2
.5
-2.0
ceiling
+inf.0
+inf.0
procedure
truncate
or/c
integer?
+inf.0
-i
nf.0
+nan.0
real?
Returns the integer farthest from
that is not farther from
than
, but
+inf.0
-i
nf.0
, and
+nan.0
truncate to themselves.
Examples:
truncate
17/4
truncate
-1
7/4
-4
truncate
2.5
2.0
truncate
-2
.5
-2.0
truncate
+inf.0
+inf.0
procedure
numerator
integer?
rational?
Coerces
to an exact number, finds the numerator of the
number expressed in its simplest fractional form, and returns this
number coerced to the exactness of
Examples:
numerator
numerator
17/4
17
numerator
2.3
2589569785738035.0
procedure
denominator
and/c
integer?
positive?
rational?
Coerces
to an exact number, finds the denominator of the
number expressed in its simplest fractional form, and returns this
number coerced to the exactness of
Examples:
denominator
denominator
17/4
denominator
2.3
1125899906842624.0
procedure
rationalize
tolerance
real?
real?
tolerance
real?
Among the real numbers within
abs
tolerance
of
returns the one corresponding to an exact number whose
denominator
is the smallest. If multiple integers are within
tolerance
of
, the one closest to
is
used.
Examples:
rationalize
1/4
1/10
1/3
rationalize
-1
/4
1/10
-1/3
rationalize
1/4
1/4
rationalize
11/40
1/4
1/2
4.3.2.2
Number Comparison
procedure
...
boolean?
number?
number?
Returns
#t
if all of the arguments are numerically equal,
#f
otherwise. An inexact number is numerically equal to an
exact number when the exact coercion of the inexact number is the
exact number. Also,
0.0
and
-0
.0
are numerically
equal, but
+nan.0
is not numerically equal to itself.
Examples:
1.0
#t
#f
2+3i
2+3i
2+3i
#t
#t
Changed in version 7.0.0.13 of package
base
: Allow one argument, in addition to allowing two or more.
procedure
...
boolean?
real?
real?
Returns
#t
if
the arguments in the given order are strictly increasing,
#f
otherwise.
Examples:
#f
#t
#t
+inf.0
#t
+nan.0
#f
Changed in version 7.0.0.13 of package
base
: Allow one argument, in addition to allowing two or more.
procedure
<=
...
boolean?
real?
real?
Returns
#t
if the arguments in the given order are non-decreasing,
#f
otherwise.
Examples:
<=
#t
<=
#f
Changed in version 7.0.0.13 of package
base
: Allow one argument, in addition to allowing two or more.
procedure
...
boolean?
real?
real?
Returns
#t
if
the arguments in the given order are strictly decreasing,
#f
otherwise.
Examples:
#f
#t
+inf.0
#t
+nan.0
#f
Changed in version 7.0.0.13 of package
base
: Allow one argument, in addition to allowing two or more.
procedure
>=
...
boolean?
real?
real?
Returns
#t
if the arguments in the given order are non-increasing,
#f
otherwise.
Examples:
>=
#t
>=
#f
Changed in version 7.0.0.13 of package
base
: Allow one argument, in addition to allowing two or more.
4.3.2.3
Powers and Roots
procedure
sqrt
number?
number?
Returns the principal
square root of
. The
result is exact if
is exact and
’s square root
is rational. See also
integer-sqrt
Examples:
sqrt
4/9
2/3
sqrt
1.4142135623730951
sqrt
-1
0+1i
procedure
integer-sqrt
complex?
integer?
Returns
floor
sqrt
for positive
. The
result is exact if
is exact. For
negative
, the result is
integer-sqrt
0+1i
Examples:
integer-sqrt
4.0
2.0
integer-sqrt
integer-sqrt
-4
.0
0.0+2.0i
integer-sqrt
-4
0+2i
procedure
integer-sqrt/remainder
complex?
integer?
integer?
Returns
integer-sqrt
and
expt
integer-sqrt
Examples:
integer-sqrt/remainder
4.0
2.0
0.0
integer-sqrt/remainder
procedure
expt
number?
number?
number?
Returns
raised to the power of
If
is
exact
, the result is exact
If
is
0.0
or
-0
.0
and
is a
real number
other than exact
or
, the
result is
1.0
(even if
is
+nan.0
).
If
is exact
, the result is exact
If
is
1.0
and
is a
real number
, the
result is
1.0
(even if
is
+nan.0
).
If
is exact
, the result is as follows:
is exact
result is
is
0.0
or
-0
.0
result is
1.0
real part of
is negative —
the
exn:fail:contract:divide-by-zero
exception is raised
is nonreal with a nonpositive real part —
the
exn:fail:contract:divide-by-zero
exception is raised
is
+nan.0
result is
+nan.0
otherwise —
result is
If
is exact
1/2
, the result is the same as
sqrt
which can be exact. Other fractional powers are not treated specially in this manner:
Examples:
expt
1/2
expt
0.5
3.0
expt
16
1/4
2.0
expt
16
0.25
2.0
Further special cases when
is a
real number
These special cases correspond to
pow
in C99 [
C99
],
except when
is negative and
is a not an
integer.
expt
0.0
is negative —
result is
+inf.0
is positive —
result is
0.0
expt
-0
.0
is negative:
is an odd integer —
result is
-i
nf.0
otherwise rational —
result is
+inf.0
is positive:
is an odd integer —
result is
-0
.0
otherwise rational —
result is
0.0
expt
-i
nf.0
for positive
is less than
1.0
result is
+inf.0
is greater than
1.0
result is
0.0
expt
+inf.0
for positive
is less than
1.0
result is
0.0
is greater than
1.0
result is
+inf.0
expt
-i
nf.0
for integer
is negative:
is odd —
result is
-0
.0
is even —
result is
0.0
is positive:
is odd —
result is
-i
nf.0
is even —
result is
+inf.0
expt
+inf.0
is negative —
result is
0.0
is positive —
result is
+inf.0
Examples:
expt
expt
0.5
2.0
expt
+inf.0
procedure
exp
number?
number?
Returns Euler’s number raised to the power of
. The result
is normally inexact, but it is exact
when
is an
exact
. See also
expt
Examples:
exp
2.718281828459045
exp
2+3i
-7.315110094901103+1.0427436562359045i
exp
procedure
log
number?
number?
number?
exp
Returns the natural logarithm of
. The result is normally
inexact, but it is exact
when
is an exact
. When
is exact
exn:fail:contract:divide-by-zero
exception is raised.
If
is provided, it serves as an alternative
base. It is equivalent to
log
log
, but
can potentially run faster. If
is exact
exn:fail:contract:divide-by-zero
exception is raised.
Consider using
fllogb
from
math/flonum
instead when accuracy is important.
Examples:
log
exp
1.0
log
2+3i
1.2824746787307684+0.982793723247329i
log
log
100
10
2.0
log
3.0
log
1.0
Changed in version 6.9.0.1 of package
base
: Added second argument for arbitrary bases.
4.3.2.4
Trigonometric Functions
procedure
sin
number?
number?
Returns the sine of
, where
is in radians. The
result is normally inexact, but it is exact
if
is exact
Examples:
sin
3.14159
2.65358979335273e-6
sin
1.0+5.0i
62.44551846769654+40.0921657779984i
procedure
cos
number?
number?
Returns the cosine of
, where
is in radians.
Examples:
cos
3.14159
-0.9999999999964793
cos
1.0+5.0i
40.09580630629883-62.43984868079963i
procedure
tan
number?
number?
Returns the tangent of
, where
is in radians. The
result is normally inexact, but it is exact
if
is exact
Examples:
tan
0.7854
1.0000036732118496
tan
1.0+5.0i
8.256719834229597e-5+1.0000377833796008i
procedure
asin
number?
number?
Returns the arcsine in radians of
. The result is normally
inexact, but it is exact
if
is exact
Examples:
asin
0.25
0.25268025514207865
asin
1.0+5.0i
0.1937931365549322+2.3309746530493123i
procedure
acos
number?
number?
Returns the arccosine in radians of
Examples:
acos
0.25
1.318116071652818
acos
1.0+5.0i
1.3770031902399644-2.3309746530493123i
procedure
atan
number?
number?
atan
number?
real?
real?
In the one-argument case, returns the arctangent of the inexact
approximation of
, except that the result is an exact
for
as
, and the
exn:fail:contract:divide-by-zero
exception is raised
for
as exact
0+1i
or exact
0-1i
In the two-argument case, the result is roughly the same as
atan
exact->inexact
exact->inexact
, but the signs of
and
determine the quadrant of the result. Moreover, a
suitable angle is returned when
divided by
produces
+nan.0
in the case that neither
nor
is
+nan.0
. Finally, if
is exact
and
is a positive number, the result is
exact
. If both
and
are exact
, the
exn:fail:contract:divide-by-zero
exception is raised.
Examples:
atan
0.5
0.4636476090008061
atan
1.1071487177940904
atan
-2
-1
-2.0344439357957027
atan
1.0+5.0i
1.530881333938778+0.19442614214700213i
atan
+inf.0
-i
nf.0
2.356194490192345
Changed in version 7.2.0.2 of package
base
: Changed to raise
exn:fail:contract:divide-by-zero
for
0+1i
and
0-1i
and to produce exact
for any positive
(not just exact values) when
is
4.3.2.5
Complex Numbers
procedure
make-rectangular
number?
real?
real?
Creates a complex number with
as the real part
and
as the imaginary part. That is, returns
0+1i
Example:
make-rectangular
4.0
3.0+4.0i
procedure
make-polar
magnitude
angle
number?
magnitude
real?
angle
real?
Creates a complex number which, if thought of as a point,
is
magnitude
away from the origin and is rotated
angle
radians counter clockwise from the positive x-axis.
That is, returns
magnitude
cos
angle
magnitude
sin
angle
0+1i
Examples:
make-polar
10
pi
1/2
6.123233995736766e-16+10.0i
make-polar
10
pi
1/4
7.0710678118654755+7.071067811865475i
procedure
real-part
real?
number?
Returns the real part of the complex number
in rectangle
coordinates.
Examples:
real-part
3+4i
real-part
5.0
5.0
procedure
imag-part
real?
number?
Returns the imaginary part of the complex number
in
rectangle coordinates.
Examples:
imag-part
3+4i
imag-part
5.0
imag-part
5.0+0.0i
0.0
procedure
magnitude
and/c
real?
not/c
negative?
number?
Returns the magnitude of the complex number
in polar
coordinates. A complex number with
+inf.0
or
-i
nf.0
as a component has magnitude
+inf.0
, even if the other
component is
+nan.0
Examples:
magnitude
-3
magnitude
3.0
3.0
magnitude
3+4i
Changed in version 7.2.0.2 of package
base
: Changed to always return
+inf.0
for a complex number with a
+inf.0
or
-i
nf.0
component.
procedure
angle
real?
number?
Returns the angle of
the complex number
in polar coordinates.
The result is guaranteed to be between
pi
and
pi
, possibly equal to
pi
(but never equal
to
pi
).
Examples:
angle
-3
3.141592653589793
angle
3.0
angle
3+4i
0.9272952180016122
angle
+inf.0+inf.0i
0.7853981633974483
angle
-1
3.141592653589793
4.3.2.6
Bitwise Operations
procedure
bitwise-ior
...
exact-integer?
exact-integer?
Returns
the bitwise “inclusive or” of the
s in their (semi-infinite)
two’s complement representation. If no arguments are provided, the
result is
Examples:
bitwise-ior
bitwise-ior
-3
-31
procedure
bitwise-and
...
exact-integer?
exact-integer?
Returns
the bitwise “and” of the
s in their (semi-infinite) two’s
complement representation. If no arguments are provided, the result
is
-1
Examples:
bitwise-and
bitwise-and
-3
-1
-32
procedure
bitwise-xor
...
exact-integer?
exact-integer?
Returns
the bitwise “exclusive or” of the
s in their (semi-infinite)
two’s complement representation. If no arguments are provided, the
result is
Examples:
bitwise-xor
bitwise-xor
-3
-1
31
procedure
bitwise-not
exact-integer?
exact-integer?
Returns the
bitwise “not” of
in its (semi-infinite) two’s complement
representation.
Examples:
bitwise-not
-6
bitwise-not
-1
procedure
bitwise-bit-set?
boolean?
exact-integer?
exact-nonnegative-integer?
Returns
#t
when the
th bit of
is set in
’s
(semi-infinite) two’s complement representation.
This operation is equivalent to
not
zero?
bitwise-and
arithmetic-shift
but it is faster and runs in constant time when
is positive.
Examples:
bitwise-bit-set?
#t
bitwise-bit-set?
#t
bitwise-bit-set?
-5
expt
700
#t
procedure
bitwise-first-bit-set
exact-integer?
exact-integer?
Returns
-1
if
is
, otherwise returns the
smallest
for which
bitwise-bit-set?
produces
#t
Example:
bitwise-first-bit-set
128
Added in version 8.16.0.4 of package
base
procedure
bitwise-bit-field
start
end
exact-integer?
exact-integer?
start
exact-nonnegative-integer?
end
and/c
exact-nonnegative-integer?
>=/c
start
Extracts the bits between position
start
and
end
(inclusive)
from
and shifts them down to the least significant portion of the number.
This operation is equivalent to the computation
bitwise-and
sub1
arithmetic-shift
end
start
arithmetic-shift
start
but it runs in constant time when
is positive,
start
and
end
are fixnums, and
end
start
is no more than
the maximum width of a fixnum.
Each pair of examples below uses the same numbers, showing the result
both in binary and as integers.
Examples:
format
"~b"
bitwise-bit-field
string->number
"1101"
"0"
bitwise-bit-field
13
format
"~b"
bitwise-bit-field
string->number
"1101"
"10"
bitwise-bit-field
13
format
"~b"
bitwise-bit-field
string->number
"1101"
"110"
bitwise-bit-field
13
procedure
arithmetic-shift
exact-integer?
exact-integer?
exact-integer?
Returns the bitwise “shift” of
in its
(semi-infinite) two’s complement representation. If
is
non-negative, the integer
is shifted left by
bits;
i.e.,
new zeros are introduced as rightmost digits. If
is negative,
is shifted right by
bits; i.e., the rightmost
digits are dropped.
Examples:
arithmetic-shift
10
1024
arithmetic-shift
255
-3
31
procedure
integer-length
exact-integer?
exact-integer?
Returns
the number of bits in the (semi-infinite) two’s complement
representation of
after removing all leading zeros (for
non-negative
) or ones (for negative
).
Examples:
integer-length
integer-length
-8
4.3.2.7
Random Numbers
procedure
random
rand-gen
exact-nonnegative-integer?
integer-in
4294967087
rand-gen
pseudo-random-generator?
current-pseudo-random-generator
random
min
max
rand-gen
exact-integer?
min
exact-integer?
max
integer-in
min
4294967087
min
rand-gen
pseudo-random-generator?
current-pseudo-random-generator
random
rand-gen
and/c
real?
inexact?
>/c
rand-gen
pseudo-random-generator?
current-pseudo-random-generator
When called with an integer argument
, returns a random
exact integer in the range
to
When called with two integer arguments
min
and
max
, returns a
random exact integer in the range
min
to
max
When called with zero arguments, returns a random inexact number between
and
, exclusive.
In each case, the number is provided by the given pseudo-random number
generator (which defaults to the current one, as produced by
current-pseudo-random-generator
). The generator maintains an
internal state for generating numbers. The random number generator
uses L’Ecuyer’s MRG32k3a algorithm [
L'Ecuyer02
] that has a
state space of practically 192 bits.
When security is a concern, use
crypto-random-bytes
instead of
random
The
math/base
library provides
additional functions for random number generation
without the limit of
4294967087
Changed in version 6.4 of package
base
: Added support for ranges.
procedure
random-seed
void?
integer-in
sub1
expt
31
Seeds the current pseudo-random number generator with
. Seeding a generator sets its internal state
deterministically; that is, 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.
The
random-seed
function is convenient for some purposes, but
note that the space of states for a pseudo-random number generator is
much larger that the space of allowed values for
. Use
vector->pseudo-random-generator!
to set a pseudo-random
number generator to any of its possible states.
procedure
make-pseudo-random-generator
pseudo-random-generator?
Returns a new pseudo-random number generator. The new generator is
seeded with a number derived from
current-milliseconds
procedure
pseudo-random-generator?
boolean?
any/c
Returns
#t
if
is a pseudo-random number generator,
#f
otherwise.
parameter
current-pseudo-random-generator
pseudo-random-generator?
current-pseudo-random-generator
rand-gen
void?
rand-gen
pseudo-random-generator?
parameter
that determines the pseudo-random number generator
used by
random
procedure
pseudo-random-generator->vector
rand-gen
pseudo-random-generator-vector?
rand-gen
pseudo-random-generator?
Produces a vector that represents the complete internal state of
rand-gen
. The vector is suitable as an argument to
vector->pseudo-random-generator
to recreate the generator in
its current state (across runs and across platforms).
procedure
vector->pseudo-random-generator
vec
pseudo-random-generator?
vec
pseudo-random-generator-vector?
Produces a pseudo-random number generator whose internal state
corresponds to
vec
procedure
vector->pseudo-random-generator!
rand-gen
vec
void?
rand-gen
pseudo-random-generator?
vec
pseudo-random-generator-vector?
Like
vector->pseudo-random-generator
, but changes
rand-gen
to the given state, instead of creating a new
generator.
procedure
pseudo-random-generator-vector?
boolean?
any/c
Returns
#t
if
is a vector of six exact integers,
where the first three integers are in the range
to
4294967086
, inclusive; the last three integers are in the
range
to
4294944442
, inclusive; at least one of
the first three integers is non-zero; and at least one of the last
three integers is non-zero. Otherwise, the result is
#f
4.3.2.8
Other Randomness Utilities
require
racket/random
package:
base
procedure
crypto-random-bytes
bytes?
exact-positive-integer?
Provides an interface to randomness from the underlying operating system. Use
crypto-random-bytes
instead of
random
wherever security is a
concern.
Returns
random bytes. On Unix systems, the bytes are
obtained from
"/dev/urandom"
, while Windows uses
the
RtlGenRand
system function.
Example:
crypto-random-bytes
14
#"\0\1\1\2\3\5\b\r\25\"7Y\220\351"
Added in version 6.3 of package
base
procedure
random-ref
seq
rand-gen
any/c
seq
sequence?
rand-gen
pseudo-random-generator?
current-pseudo-random-generator
Returns a random element of the sequence. Like
sequence-length
, does
not terminate on infinite sequences, and evaluates the entire sequence.
Added in version 6.4 of package
base
procedure
random-sample
seq
rand-gen
#:replacement?
replacement?
listof
any/c
seq
sequence?
exact-positive-integer?
rand-gen
pseudo-random-generator?
current-pseudo-random-generator
replacement?
any/c
#t
Returns a list of
elements of
seq
, picked at random, listed
in any order.
If
replacement?
is non-false, elements are drawn with replacement,
which allows for duplicates.
Like
sequence-length
, does not terminate on infinite sequences, and
evaluates the entire sequence.
Added in version 6.4 of package
base
4.3.2.9
Number–String Conversions
procedure
number->string
radix
string?
number?
radix
or/c
10
16
10
Returns a string that is the printed form of
(see
Printing Numbers
in the base specified by
radix
. If
is inexact,
radix
must be
10
, otherwise the
exn:fail:contract
exception is raised.
Examples:
number->string
3.0
"3.0"
number->string
255
"377"
procedure
string->number
radix
convert-mode
decimal-mode
single-mode
or/c
number?
#f
string?
extflonum?
string?
radix
integer-in
16
10
convert-mode
or/c
number-or-false
read
number-or-false
decimal-mode
or/c
decimal-as-inexact
decimal-as-exact
if
read-decimal-as-inexact
decimal-as-inexact
decimal-as-exact
single-mode
or/c
single
double
if
read-single-flonum
single
double
Reads and returns a number datum from
(see
Reading Numbers
). The optional
radix
argument
specifies the default base for the number, which can be overridden by
#b
#o
#d
, or
#x
in the
string.
If
convert-mode
is
number-or-false
, the result is
#f
if
does not parse exactly as a number datum
(with no whitespace). If
convert-mode
is
read
, the
result can be an
extflonum
, and it can be a string that
contains an error message if
read
of
would report
a reader exception (but the result can still be
#f
if
read
would report a symbol).
The
decimal-mode
argument controls number parsing the same
way that the
read-decimal-as-inexact
parameter affects
read
The
single-mode
argument controls number parsing the same way
that the
read-single-flonum
parameter affects
read
Examples:
string->number
"3.0+2.5i"
3.0+2.5i
string->number
"hello"
#f
string->number
"111"
57
string->number
"#b111"
string->number
"#e+inf.0"
10
read
"no exact representation for +inf.0"
string->number
"10.3"
10
read
decimal-as-exact
103/10
Changed in version 6.8.0.2 of package
base
: Added the
convert-mode
and
decimal-mode
arguments.
Changed in version 7.3.0.5: Added the
single-mode
argument.
procedure
real->decimal-string
decimal-digits
string?
rational?
decimal-digits
exact-nonnegative-integer?
Prints
into a string and returns the string. The printed
form of
shows exactly
decimal-digits
digits after
the decimal point. The printed form uses a minus sign if
is
negative, and it does not use a plus sign if
is positive.
Before printing,
is converted to an exact number,
multiplied by
expt
10
decimal-digits
, rounded, and then
divided again by
expt
10
decimal-digits
. The result of this
process is an exact number whose decimal representation has no more
than
decimal-digits
digits after the decimal (and it is
padded with trailing zeros if necessary).
If
is a real number with no decimal representation (e.g.
+nan.0
+inf.0
), then the
exn:fail:contract
exception is raised.
(Any real number that is convertible to decimal notation is rational,
so
must be
rational?
, despite the name of the
function.)
Examples:
real->decimal-string
pi
"3.14"
real->decimal-string
pi
"3.14159"
procedure
integer-bytes->integer
bstr
signed?
big-endian?
start
end
exact-integer?
bstr
bytes?
signed?
any/c
big-endian?
any/c
system-big-endian?
start
exact-nonnegative-integer?
end
exact-nonnegative-integer?
bytes-length
bstr
Converts the machine-format number encoded in
bstr
to an
exact integer. The
start
and
end
arguments specify
the substring to decode, where
end
start
must be
, or
. If
signed?
is true,
then the bytes are decoded as a two’s-complement number, otherwise it
is decoded as an unsigned integer. If
big-endian?
is true,
then the first byte’s value provides the most significant
eight bits of the number, otherwise the first byte provides the
least-significant eight bits, and so on.
Changed in version 6.10.0.1 of package
base
: Added support for decoding a 1-byte string.
procedure
integer->integer-bytes
size-n
signed?
big-endian?
dest-bstr
start
bytes?
exact-integer?
size-n
or/c
signed?
any/c
big-endian?
any/c
system-big-endian?
dest-bstr
and/c
bytes?
not/c
immutable?
make-bytes
size-n
start
exact-nonnegative-integer?
Converts the exact integer
to a machine-format number
encoded in a byte string of length
size-n
, which must be
, or
. If
signed?
is true,
then the number is encoded as two’s complement, otherwise it is
encoded as an unsigned bit stream. If
big-endian?
is true,
then the most significant eight bits of the number are encoded in the
first byte of the resulting byte string, otherwise the
least-significant bits are encoded in the first byte, and so on.
The
dest-bstr
argument must be a mutable byte string of
length
size-n
. The encoding of
is written into
dest-bstr
starting at offset
start
, and
dest-bstr
is returned as the result.
If
cannot be encoded in a byte string of the requested size and
format, the
exn:fail:contract
exception is raised. If
dest-bstr
is not
of length
size-n
, the
exn:fail:contract
exception is raised.
Changed in version 6.10.0.1 of package
base
: Added support for encoding a 1-byte value.
procedure
floating-point-bytes->real
bstr
big-endian?
start
end
flonum?
bstr
bytes?
big-endian?
any/c
system-big-endian?
start
exact-nonnegative-integer?
end
exact-nonnegative-integer?
bytes-length
bstr
Converts the IEEE floating-point number encoded in
bstr
from
position
start
(inclusive) to
end
(exclusive) to an
inexact real number. The difference between
start
an
end
must be either 4 or 8 bytes. If
big-endian?
is
true, then the first byte’s ASCII value provides the most significant
eight bits of the IEEE representation, otherwise the first byte
provides the least-significant eight bits, and so on.
procedure
real->floating-point-bytes
size-n
big-endian?
dest-bstr
start
bytes?
real?
size-n
or/c
big-endian?
any/c
system-big-endian?
dest-bstr
and/c
bytes?
not/c
immutable?
make-bytes
size-n
start
exact-nonnegative-integer?
Converts the real number
to its IEEE representation in a
byte string of length
size-n
, which must be
or
. If
big-endian?
is true, then the most significant
eight bits of the number are encoded in the first byte of the
resulting byte string, otherwise the least-significant bits are
encoded in the first character, and so on.
The
dest-bstr
argument must be a mutable byte string of
length
size-n
. The encoding of
is written into
dest-bstr
starting with byte
start
, and
dest-bstr
is returned as the result.
If
dest-bstr
is provided and it has less than
start
plus
size-n
bytes, the
exn:fail:contract
exception is raised.
procedure
system-big-endian?
boolean?
Returns
#t
if the native encoding of numbers is big-endian
for the machine running Racket,
#f
if the native encoding
is little-endian.
4.3.2.10
Extra Constants and Functions
require
racket/math
package:
base
The bindings documented in this section are provided by the
racket/math
and
racket
libraries, but not
racket/base
value
pi
flonum?
An approximation of π, the ratio of a circle’s circumference to its
diameter.
Examples:
pi
3.141592653589793
cos
pi
-1.0
value
pi.f
or/c
single-flonum?
flonum?
The same value as
pi
, but as a single-precision
floating-point number if the current platform supports it.
Changed in version 7.3.0.5 of package
base
: Allow value to be a double-precision flonum.
procedure
degrees->radians
real?
real?
Converts an
-degree angle to radians.
Examples:
degrees->radians
180
3.141592653589793
sin
degrees->radians
45
0.7071067811865475
procedure
radians->degrees
real?
real?
Converts
radians to degrees.
Examples:
radians->degrees
pi
180.0
radians->degrees
1/4
pi
45.0
procedure
sqr
number?
number?
Returns
procedure
sgn
or/c
=/c
-1
=/c
=/c
+nan.0
+nan.f
real?
Returns the sign of
as either
(or a signed-zero variant, when
inexact),
, or not-a-number.
Examples:
sgn
10
sgn
-1
0.0
-1.0
sgn
sgn
-0
.0
-0.0
sgn
0.0
0.0
sgn
+nan.0
+nan.0
sgn
+inf.0
1.0
sgn
-i
nf.0
-1.0
procedure
conjugate
number?
number?
Returns the complex conjugate of
Examples:
conjugate
conjugate
3+4i
3-4i
procedure
sinh
number?
number?
Returns the hyperbolic sine of
procedure
cosh
number?
number?
Returns the hyperbolic cosine of
procedure
tanh
number?
number?
Returns the hyperbolic tangent of
procedure
exact-round
exact-integer?
rational?
Equivalent to
inexact->exact
round
procedure
exact-floor
exact-integer?
rational?
Equivalent to
inexact->exact
floor
procedure
exact-ceiling
exact-integer?
rational?
Equivalent to
inexact->exact
ceiling
procedure
exact-truncate
exact-integer?
rational?
Equivalent to
inexact->exact
truncate
procedure
order-of-magnitude
and/c
exact?
integer?
and/c
real?
positive?
Computes the greatest exact integer
such that:
<=
expt
10
inexact->exact
Hence also:
inexact->exact
expt
10
add1
Examples:
order-of-magnitude
999
order-of-magnitude
1000
order-of-magnitude
1/100
-2
order-of-magnitude
1/101
-3
procedure
nan?
boolean?
real?
Returns
#t
if
is
eqv?
to
+nan.0
or
+nan.f
; otherwise
#f
procedure
infinite?
boolean?
real?
Returns
#t
if
is
+inf.0
-i
nf.0
+inf.f
-inf.f
; otherwise
#f
procedure
positive-integer?
boolean?
any/c
Like
exact-positive-integer?
, but also returns
#t
for positive
inexact?
integers.
Added in version 6.8.0.2 of package
base
procedure
negative-integer?
boolean?
any/c
The same as
and
integer?
negative?
Added in version 6.8.0.2 of package
base
procedure
nonpositive-integer?
boolean?
any/c
The same as
and
integer?
not
positive?
Added in version 6.8.0.2 of package
base
procedure
nonnegative-integer?
boolean?
any/c
Like
exact-nonnegative-integer?
, but also returns
#t
for non-negative
inexact?
integers.
Added in version 6.8.0.2 of package
base
procedure
natural?
boolean?
any/c
An alias for
exact-nonnegative-integer?
Added in version 6.8.0.2 of package
base
top
contents
← prev
up
next →
US