4.2.2 Generic Numerics
Most Racket numeric operations work on any kind of number.
4.2.2.1 Arithmetic
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.
> (quotient 10 3) 3
> (quotient -10.0 3) -3.0
> (quotient +inf.0 3) quotient: contract violation
expected: integer?
given: +inf.0
argument position: 1st
other arguments...:
3
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
> (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: contract violation
expected: integer?
given: +inf.0
argument position: 1st
other arguments...:
3
procedure
(quotient/remainder n m) →
integer? integer? n : integer? m : integer?
> (quotient/remainder 10 3)
3
1
(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.
> (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: contract violation
expected: integer?
given: +inf.0
argument position: 1st
other arguments...:
3
> (truncate 17/4) 4
> (truncate -17/4) -4
> (truncate 2.5) 2.0
> (truncate -2.5) -2.0
> (truncate +inf.0) +inf.0
procedure
(denominator q) → integer?
q : rational?
> (denominator 5) 1
> (denominator 17/4) 4
> (denominator 2.3) 1125899906842624.0
procedure
(rationalize x tolerance) → real?
x : real? tolerance : real?
> (rationalize 1/4 1/10) 1/3
> (rationalize -1/4 1/10) -1/3
> (rationalize 1/4 1/4) 0
> (rationalize 11/40 1/4) 1/2
4.2.2.2 Number Comparison
4.2.2.3 Powers and Roots
procedure
(integer-sqrt n) → complex?
n : integer?
> (integer-sqrt 4.0) 2.0
> (integer-sqrt 5) 2
> (integer-sqrt -4.0) 0+2.0i
> (integer-sqrt -4) 0+2i
procedure
(integer-sqrt/remainder n) →
complex? integer? n : integer?
> (integer-sqrt/remainder 4.0)
2.0
0.0
> (integer-sqrt/remainder 5)
2
1
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
4.2.2.4 Trigonometric Functions
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.
> (atan 0.5) 0.4636476090008061
> (atan 2 1) 1.1071487177940904
> (atan -2 -1) -2.0344439357957027
> (atan 1.0+5.0i) 1.530881333938778+0.19442614214700213i
> (atan +inf.0 -inf.0) 2.356194490192345
4.2.2.5 Complex Numbers
procedure
(make-rectangular x y) → number?
x : real? y : real?
> (make-rectangular 3 4.0) 3.0+4.0i
procedure
(make-polar magnitude angle) → number?
magnitude : real? angle : real?
> (make-polar 10 (* pi 1/2)) 6.123233995736766e-16+10.0i
> (make-polar 10 (* pi 1/4)) 7.0710678118654755+7.071067811865475i
The result is guaranteed to be between (- pi) and pi, possibly equal to pi (but never equal to (- pi)).
> (angle -3) 3.141592653589793
> (angle 3.0) 0
> (angle 3+4i) 0.9272952180016122
> (angle +inf.0+inf.0i) 0.7853981633974483
> (angle -1) 3.141592653589793
4.2.2.6 Bitwise Operations
procedure
(bitwise-ior n ...) → exact-integer?
n : exact-integer?
> (bitwise-ior 1 2) 3
> (bitwise-ior -32 1) -31
procedure
(bitwise-and n ...) → exact-integer?
n : exact-integer?
> (bitwise-and 1 2) 0
> (bitwise-and -32 -1) -32
procedure
(bitwise-xor n ...) → exact-integer?
n : exact-integer?
> (bitwise-xor 1 5) 4
> (bitwise-xor -32 -1) 31
procedure
(bitwise-not n) → exact-integer?
n : exact-integer?
> (bitwise-not 5) -6
> (bitwise-not -1) 0
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.
> (bitwise-bit-set? 5 0) #t
> (bitwise-bit-set? 5 2) #t
> (bitwise-bit-set? -5 (expt 2 700)) #t
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.
> (format "~b" (bitwise-bit-field (string->number "1101" 2) 1 1)) "0"
> (bitwise-bit-field 13 1 1) 0
> (format "~b" (bitwise-bit-field (string->number "1101" 2) 1 3)) "10"
> (bitwise-bit-field 13 1 3) 2
> (format "~b" (bitwise-bit-field (string->number "1101" 2) 1 4)) "110"
> (bitwise-bit-field 13 1 4) 6
procedure
(arithmetic-shift n m) → exact-integer?
n : exact-integer? m : exact-integer?
> (arithmetic-shift 1 10) 1024
> (arithmetic-shift 255 -3) 31
procedure
(integer-length n) → exact-integer?
n : exact-integer?
> (integer-length 8) 4
> (integer-length -8) 3
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 min max [rand-gen]) → exact-nonnegative-integer? min : (integer-in 1 4294967087) max : (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)
When called with two integer arguments min and max, returns a random exact integer in the range min to max-1.
When called with zero arguments, returns a random inexact number between 0 and 1, 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 a 54-bit version of L’Ecuyer’s MRG32k3a algorithm [L'Ecuyer02].
Changed in version 6.4 of package base: Added support for ranges.
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 Other Randomness Utilities
(require racket/random) | package: base |
procedure
(crypto-random-bytes n) → bytes?
n : exact-positive-integer?
Returns n random bytes. On Unix systems, the bytes are obtained from "/dev/urandom", while Windows uses the RtlGenRand system function.
> (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)
Added in version 6.4 of package base.
procedure
(random-sample seq n [ rand-gen #:replacement? replacement?]) → (listof any/c) seq : sequence? n : exact-positive-integer?
rand-gen : pseudo-random-generator? = (current-pseudo-random-generator) replacement? : any/c = #t
Like sequence-length, does not terminate on infinite sequences, and evaluates the entire sequence.
Added in version 6.4 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
> (number->string 3.0) "3.0"
> (number->string 255 8) "377"
procedure
(string->number s [radix]) → (or/c number? #f)
s : string? radix : (integer-in 2 16) = 10
> (string->number "3.0+2.5i") 3.0+2.5i
> (string->number "hello") #f
> (string->number "111" 7) 57
> (string->number "#b111" 7) 7
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).
> (real->decimal-string pi) "3.14"
> (real->decimal-string pi 5) "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? = 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?
> (degrees->radians 180) 3.141592653589793
> (sin (degrees->radians 45)) 0.7071067811865475
procedure
(radians->degrees x) → real?
x : real?
> (radians->degrees pi) 180.0
> (radians->degrees (* 1/4 pi)) 45.0
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)))
> (order-of-magnitude 999) 2
> (order-of-magnitude 1000) 3
> (order-of-magnitude 1/100) -2
> (order-of-magnitude 1/101) -3