4.2.2 Generic Numerics
Most Racket numeric operations work on any kind of number.
4.2.2.1 Arithmetic
Examples: | ||||||
|
Examples: | ||||||
|
Examples: | ||||||
|
If z is exact 0 and no w is exact 0, then the result is exact 0. If any w is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
Examples: | ||||||||
|
Examples: | |||||||||||
|
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
Examples: | |||||||||||||||
|
procedure
(quotient/remainder n m) →
integer? integer? n : integer? m : integer?
Example: | ||||
|
(abs q) is between 0 (inclusive) and (abs m) (exclusive), and
the difference between q and (- n (* m (quotient n m))) is a multiple of m.
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
Examples: | |||||||||||||||
|
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||||
|
Examples: | ||||||
|
Examples: | ||||||||||
|
Examples: | ||||||||||
|
Examples: | ||||||||||
|
Examples: | ||||||||||
|
Examples: | ||||||
|
procedure
(denominator q) → integer?
q : rational?
Examples: | ||||||
|
procedure
(rationalize x tolerance) → real?
x : real? tolerance : real?
Examples: | ||||||||
|
4.2.2.2 Number Comparison
Examples: | ||||||
|
Examples: | ||||||||
|
Examples: | ||||
|
Examples: | ||||||||
|
Examples: | ||||
|
4.2.2.3 Powers and Roots
Examples: | ||||||
|
procedure
(integer-sqrt n) → complex?
n : integer?
Examples: | ||||||||
|
procedure
(integer-sqrt/remainder n) →
complex? integer? n : integer?
Examples: | ||||||||
|
If w is exact 0, the result is exact 1. If w is 0.0 or -0.0 and z is a real number, the result is 1.0 (even if z is +nan.0).
If z is exact 1, the result is exact 1. If z is 1.0 and w is a real number, the result is 1.0 (even if w is +nan.0).
If z is exact 0 and w is negative, the exn:fail:contract:divide-by-zero exception is raised.
w is negative —
+inf.0 w is positive —
0.0
- w is negative:
w is an odd integer —
-inf.0 w otherwise rational —
+inf.0
- w is positive:
w is an odd integer —
-0.0 w otherwise rational —
0.0
z is less than 1.0 —
+inf.0 z is greater than 1.0 —
0.0
z is less than 1.0 —
0.0 z is greater than 1.0 —
+inf.0
- w is negative:
w is odd —
-0.0 w is even —
0.0
- w is positive:
w is odd —
-inf.0 w is even —
+inf.0
w is negative —
0.0 w is positive —
+inf.0
Examples: | ||||||
|
Examples: | ||||||
|
Examples: | ||||||
|
4.2.2.4 Trigonometric Functions
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||
|
In the two-argument case, the result is roughly the same as (atan (/ (exact->inexact y)) (exact->inexact x)), but the signs of y and x determine the quadrant of the result. Moreover, a suitable angle is returned when y divided by x produces +nan.0 in the case that neither y nor x is +nan.0. Finally, if y is exact 0 and x is an exact positive number, the result is exact 0. If both x and y are exact 0, the exn:fail:contract:divide-by-zero exception is raised.
Examples: | ||||||||||
|
4.2.2.5 Complex Numbers
procedure
(make-rectangular x y) → number?
x : real? y : real?
Example: | ||
|
procedure
(make-polar magnitude angle) → number?
magnitude : real? angle : real?
Examples: | ||||
|
Examples: | ||||
|
Examples: | ||||||
|
Examples: | ||||||
|
The result is guaranteed to be between (- pi) and pi, possibly equal to pi (but never equal to (- pi)).
Examples: | ||||||||||
|
4.2.2.6 Bitwise Operations
procedure
(bitwise-ior n ...) → exact-integer?
n : exact-integer?
Examples: | ||||
|
procedure
(bitwise-and n ...) → exact-integer?
n : exact-integer?
Examples: | ||||
|
procedure
(bitwise-xor n ...) → exact-integer?
n : exact-integer?
Examples: | ||||
|
procedure
(bitwise-not n) → exact-integer?
n : exact-integer?
Examples: | ||||
|
procedure
(bitwise-bit-set? n m) → boolean?
n : exact-integer? m : exact-nonnegative-integer?
This operation is equivalent to (not (zero? (bitwise-and n (arithmetic-shift 1 m)))), but it is faster and runs in constant time when n is positive.
Examples: | ||||||
|
procedure
(bitwise-bit-field n start end) → exact-integer?
n : exact-integer? start : exact-nonnegative-integer?
end :
(and/c exact-nonnegative-integer? (start . <= . end))
This operation is equivalent to the computation
(bitwise-and (sub1 (arithmetic-shift 1 (- end start))) (arithmetic-shift n (- start)))
but it runs in constant time when n 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: | ||||||||||||
|
procedure
(arithmetic-shift n m) → exact-integer?
n : exact-integer? m : exact-integer?
Examples: | ||||
|
procedure
(integer-length n) → exact-integer?
n : exact-integer?
Examples: | ||||
|
4.2.2.7 Random Numbers
When security is a concern, use crypto-random-bytes instead of random.
procedure
(random k [rand-gen]) → exact-nonnegative-integer?
k : (integer-in 1 4294967087)
rand-gen : pseudo-random-generator? = (current-pseudo-random-generator) (random [rand-gen]) → (and/c real? inexact? (>/c 0) (</c 1))
rand-gen : pseudo-random-generator? = (current-pseudo-random-generator)
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 a 54-bit version of L’Ecuyer’s MRG32k3a algorithm [L'Ecuyer02].
procedure
(random-seed k) → void?
k : (integer-in 1 (sub1 (expt 2 31)))
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 k. Use vector->pseudo-random-generator! to set a pseudo-random number generator to any of its possible states.
procedure
procedure
v : any/c
parameter
(current-pseudo-random-generator) → pseudo-random-generator?
(current-pseudo-random-generator rand-gen) → void? rand-gen : pseudo-random-generator?
procedure
(pseudo-random-generator->vector rand-gen)
→ pseudo-random-generator-vector? rand-gen : pseudo-random-generator?
procedure
→ pseudo-random-generator? vec : pseudo-random-generator-vector?
procedure
(vector->pseudo-random-generator! rand-gen vec) → void? rand-gen : pseudo-random-generator? vec : pseudo-random-generator-vector?
procedure
v : any/c
4.2.2.8 System-Provided Randomness
(require racket/random) | package: base |
procedure
(crypto-random-bytes n) → bytes?
n : exact-positive-integer?
Example: | ||
|
Added in version 6.3 of package base.
4.2.2.9 Number–String Conversions
procedure
(number->string z [radix]) → string?
z : number? radix : (or/c 2 8 10 16) = 10
Examples: | ||||
|
procedure
(string->number s [radix]) → (or/c number? #f)
s : string? radix : (integer-in 2 16) = 10
Examples: | ||||||||
|
procedure
(real->decimal-string n [decimal-digits]) → string?
n : real? decimal-digits : exact-nonnegative-integer? = 2
Before printing, n 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).
Examples: | ||||
|
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? = 0 end : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(integer->integer-bytes n size-n signed? [ big-endian? dest-bstr start]) → bytes? n : exact-integer? size-n : (or/c 2 4 8) 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? = 0
The dest-bstr argument must be a mutable byte string of length size-n. The encoding of n is written into dest-bstr starting at offset start, and dest-bstr is returned as the result.
If n cannot be encoded in a 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.
procedure
(floating-point-bytes->real bstr [ big-endian? start end]) → flonum? bstr : bytes? big-endian? : any/c = (system-big-endian?) start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(real->floating-point-bytes x size-n [ big-endian? dest-bstr start]) → bytes? x : real? size-n : (or/c 4 8) big-endian? : any/c = (system-big-endian?)
dest-bstr : (and/c bytes? (not/c immutable?)) = (make-bytes size-n) start : exact-nonnegative-integer? = 0
The dest-bstr argument must be a mutable byte string of length size-n. The encoding of n 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
4.2.2.10 Extra Constants and Functions
(require racket/math) | package: base |
value
procedure
(degrees->radians x) → real?
x : real?
Examples: | ||||
|
procedure
(radians->degrees x) → real?
x : real?
Examples: | ||||
|
Examples: | ||||||||
|
Examples: | ||||
|
procedure
(exact-round x) → exact-integer?
x : rational?
procedure
(exact-floor x) → exact-integer?
x : rational?
procedure
(exact-ceiling x) → exact-integer?
x : rational?
procedure
(exact-truncate x) → exact-integer?
x : rational?
(<= (expt 10 m) (inexact->exact r))
(< (inexact->exact r) (expt 10 (add1 m)))
Examples: | ||||||||
|