3.2.2 Generic Numerics
Most Racket numeric operations work on any kind of number.
3.2.2.1 Arithmetic
Examples: |
> (+ 1 2) |
3 |
> (+ 1.0 2+3i 5) |
8.0+3.0i |
> (+) |
0 |
Examples: |
> (- 5 3.0) |
2.0 |
> (- 1) |
-1 |
> (- 2+7i 1 3) |
-2+7i |
Examples: |
> (* 2 3) |
6 |
> (* 8.0 9) |
72.0 |
> (* 1+2i 3+4i) |
-5+10i |
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: |
> (/ 3 4) |
3/4 |
> (/ 81 3 3) |
9 |
> (/ 10.0) |
0.1 |
> (/ 1+2i 3+4i) |
11/25+2/25i |
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 |
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
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 |
| ||||||||
n : integer? | ||||||||
m : integer? |
Example: |
> (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.
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 |
Examples: |
> (abs 1.0) |
1.0 |
> (abs -1) |
1 |
Examples: |
> (max 1 3 2) |
3 |
> (max 1 3 2.0) |
3.0 |
Examples: |
> (min 1 3 2) |
1 |
> (min 1 3 2.0) |
1.0 |
Examples: |
> (gcd 10) |
10 |
> (gcd 12 81.0) |
3.0 |
Examples: |
> (lcm 10) |
10 |
> (lcm 3 4.0) |
12.0 |
Examples: |
> (round 17/4) |
4 |
> (round -17/4) |
-4 |
> (round 2.5) |
2.0 |
> (round -2.5) |
-2.0 |
Examples: |
> (floor 17/4) |
4 |
> (floor -17/4) |
-5 |
> (floor 2.5) |
2.0 |
> (floor -2.5) |
-3.0 |
Examples: |
> (ceiling 17/4) |
5 |
> (ceiling -17/4) |
-4 |
> (ceiling 2.5) |
3.0 |
> (ceiling -2.5) |
-2.0 |
Examples: |
> (truncate 17/4) |
4 |
> (truncate -17/4) |
-4 |
> (truncate 2.5) |
2.0 |
> (truncate -2.5) |
-2.0 |
Examples: |
> (numerator 5) |
5 |
> (numerator 17/4) |
17 |
> (numerator 2.3) |
2589569785738035.0 |
(denominator q) → integer? |
q : rational? |
Examples: |
> (denominator 5) |
1 |
> (denominator 17/4) |
4 |
> (denominator 2.3) |
1125899906842624.0 |
(rationalize x tolerance) → real? |
x : real? |
tolerance : real? |
Examples: |
> (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 |
3.2.2.2 Number Comparison
Examples: |
> (= 1 1.0) |
#t |
> (= 1 2) |
#f |
> (= 2+3i 2+3i 2+3i) |
#t |
Examples: |
> (< 1 1) |
#f |
> (< 1 2 3) |
#t |
> (< 1 +inf.0) |
#t |
> (< 1 +nan.0) |
#f |
Examples: |
> (<= 1 1) |
#t |
> (<= 1 2 1) |
#f |
Examples: |
> (> 1 1) |
#f |
> (> 3 2 1) |
#t |
> (> +inf.0 1) |
#t |
> (< +nan.0 1) |
#f |
Examples: |
> (>= 1 1) |
#t |
> (>= 1 2 1) |
#f |
3.2.2.3 Powers and Roots
Examples: |
> (sqrt 4/9) |
2/3 |
> (sqrt 2) |
1.4142135623730951 |
> (sqrt -1) |
0+1i |
(integer-sqrt n) → complex? |
n : integer? |
Examples: |
> (integer-sqrt 4.0) |
2.0 |
> (integer-sqrt 5) |
2 |
| ||||||||
n : integer? |
Examples: |
> (integer-sqrt/remainder 4.0) |
2.0 |
0.0 |
> (integer-sqrt/remainder 5) |
2 |
1 |
Examples: |
> (expt 2 3) |
8 |
> (expt 4 0.5) |
2.0 |
> (expt +inf.0 0) |
1 |
Examples: |
> (exp 1) |
2.718281828459045 |
> (exp 2+3i) |
-7.315110094901103+1.0427436562359045i |
> (exp 0) |
1 |
Examples: |
> (log (exp 1)) |
1.0 |
> (log 2+3i) |
1.2824746787307684+0.982793723247329i |
> (log 1) |
0 |
3.2.2.4 Trignometric Functions
Examples: |
> (sin 3.14159) |
2.65358979335273e-06 |
> (sin 1.0+5.0i) |
62.44551846769653+40.0921657779984i |
Examples: |
> (cos 3.14159) |
-0.9999999999964793 |
> (cos 1.0+5.0i) |
40.095806306298826-62.43984868079963i |
Examples: |
> (tan 0.7854) |
1.0000036732118496 |
> (tan 1.0+5.0i) |
8.256719834227411e-05+1.0000377833796008i |
Examples: |
> (asin 0.25) |
0.25268025514207865 |
> (asin 1.0+5.0i) |
0.1937931365549321+2.3309746530493123i |
Examples: |
> (acos 0.25) |
1.318116071652818 |
> (acos 1.0+5.0i) |
1.3770031902399644-2.3309746530493123i |
In the two-argument case, the result is roughly the same as (/ (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 x is exact 0 and y 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: |
> (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 |
3.2.2.5 Complex Numbers
(make-rectangular x y) → number? |
x : real? |
y : real? |
Example: |
> (make-rectangular 3 4.0) |
3.0+4.0i |
(make-polar magnitude angle) → number? |
magnitude : real? |
angle : real? |
Examples: |
> (make-polar 10 (* pi 1/2)) |
6.123233995736766e-16+10.0i |
> (make-polar 10 (* pi 1/4)) |
7.0710678118654755+7.071067811865475i |
Examples: |
> (real-part 3+4i) |
3 |
> (real-part 5.0) |
5.0 |
Examples: |
> (imag-part 3+4i) |
4 |
> (imag-part 5.0) |
0 |
> (imag-part 5.0+0.0i) |
0.0 |
Examples: |
> (magnitude -3) |
3 |
> (magnitude 3.0) |
3.0 |
> (magnitude 3+4i) |
5 |
Examples: |
> (angle -3) |
3.141592653589793 |
> (angle 3.0) |
0 |
> (angle 3+4i) |
0.9272952180016122 |
> (angle +inf.0+inf.0i) |
0.7853981633974483 |
3.2.2.6 Bitwise Operations
(bitwise-ior n ...) → exact-integer? |
n : exact-integer? |
Examples: |
> (bitwise-ior 1 2) |
3 |
> (bitwise-ior -32 1) |
-31 |
(bitwise-and n ...) → exact-integer? |
n : exact-integer? |
Examples: |
> (bitwise-and 1 2) |
0 |
> (bitwise-and -32 -1) |
-32 |
(bitwise-xor n ...) → exact-integer? |
n : exact-integer? |
Examples: |
> (bitwise-xor 1 5) |
4 |
> (bitwise-xor -32 -1) |
31 |
(bitwise-not n) → exact-integer? |
n : exact-integer? |
Examples: |
> (bitwise-not 5) |
-6 |
> (bitwise-not -1) |
0 |
(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: |
> (bitwise-bit-set? 5 0) |
#t |
> (bitwise-bit-set? 5 2) |
#t |
> (bitwise-bit-set? -5 (expt 2 700)) |
#t |
(bitwise-bit-field n start end) → exact-integer? | ||||||||
n : exact-integer? | ||||||||
start : exact-nonnegative-integer? | ||||||||
|
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: |
> (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 |
(arithmetic-shift n m) → exact-integer? |
n : exact-integer? |
m : exact-integer? |
Examples: |
> (arithmetic-shift 1 10) |
1024 |
> (arithmetic-shift 255 -3) |
31 |
(integer-length n) → exact-integer? |
n : exact-integer? |
Examples: |
> (integer-length 8) |
4 |
> (integer-length -8) |
3 |
3.2.2.7 Random Numbers
(random k [generator]) → exact-nonnegative-integer? | ||||||||||||
k : (integer-in 1 4294967087) | ||||||||||||
| ||||||||||||
(random [generator]) → (and/c real? inexact? (>/c 0) (</c 1)) | ||||||||||||
|
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].
(random-seed k) → void? |
k : (integer-in 1 (sub1 (expt 2 31))) |
(pseudo-random-generator? v) → boolean? |
v : any/c |
(current-pseudo-random-generator) → pseudo-random-generator? |
(current-pseudo-random-generator generator) → void? |
generator : pseudo-random-generator? |
(pseudo-random-generator->vector generator) → vector? |
generator : pseudo-random-generator? |
(vector->pseudo-random-generator vec) |
→ pseudo-random-generator? |
vec : vector? |
| ||||||||||||||
generator : pseudo-random-generator? | ||||||||||||||
vec : vector? |
3.2.2.8 Number–String Conversions
(number->string z [radix]) → string? |
z : number? |
radix : (or/c 2 8 10 16) = 10 |
Examples: |
> (number->string 3.0) |
"3.0" |
> (number->string 255 8) |
"377" |
(string->number s [radix]) → (or/c number? #f) |
s : string? |
radix : (integer-in 2 16) = 10 |
Examples: |
> (string->number "3.0+2.5i") |
3.0+2.5i |
> (string->number "hello") |
#f |
> (string->number "111" 7) |
57 |
> (string->number "#b111" 7) |
7 |
(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: |
> (real->decimal-string pi) |
"3.14" |
> (real->decimal-string pi 5) |
"3.14159" |
| |||||||||||||||||||||||||||||||||||
bstr : bytes? | |||||||||||||||||||||||||||||||||||
signed? : any/c | |||||||||||||||||||||||||||||||||||
big-endian? : any/c = (system-big-endian?) | |||||||||||||||||||||||||||||||||||
start : exact-nonnegative-integer? = 0 | |||||||||||||||||||||||||||||||||||
end : exact-nonnegative-integer? = (bytes-length bstr) |
| ||||||||||||||||||||||||||||||||||||||||||
n : exact-integer? | ||||||||||||||||||||||||||||||||||||||||||
size-n : (or/c 2 4 8) | ||||||||||||||||||||||||||||||||||||||||||
signed? : any/c | ||||||||||||||||||||||||||||||||||||||||||
big-endian? : any/c = (system-big-endian?) | ||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||
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.
| ||||||||||||||||
→ (and/c real? inexact?) | ||||||||||||||||
bstr : bytes? | ||||||||||||||||
big-endian? : any/c = (system-big-endian?) | ||||||||||||||||
start : exact-nonnegative-integer? = 0 | ||||||||||||||||
end : exact-nonnegative-integer? = (bytes-length bstr) |
| |||||||||||||||||||||||||||||||||||
x : real? | |||||||||||||||||||||||||||||||||||
size-n : (or/c 4 8) | |||||||||||||||||||||||||||||||||||
big-endian? : any/c = (system-big-endian?) | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
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.
3.2.2.9 Extra Constants and Functions
Examples: |
> (sgn 10) |
1 |
> (sgn -10.0) |
-1.0 |
> (sgn 0) |
0 |
Examples: |
> (conjugate 1) |
1 |
> (conjugate 3+4i) |
3-4i |
(<= (expt 10 m) |
(inexact->exact r)) |
(< (inexact->exact r) |
(expt 10 (add1 m))) |
Examples: |
> (order-of-magnitude 999) |
2 |
> (order-of-magnitude 1000) |
3 |
> (order-of-magnitude 1/100) |
-2 |
> (order-of-magnitude 1/101) |
-3 |