Most Racket numeric operations work on any kind of number.
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.
3
-3.0
quotient: contract violation
expected: integer?
given: +inf.0
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
1
-1.0
1.0
-1
remainder: contract violation
expected: integer?
given: +inf.0
3
1
(abs q) is between 0 (inclusive) and (abs m) (exclusive), and
the difference between q and (- n(* m(quotient nm))) is a multiple of m.
If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.
1
2.0
-2.0
-1
modulo: contract violation
expected: integer?
given: +inf.0
5
-4
3.0
-2.0
+inf.0
4
-4
2.0
-2.0
+inf.0
procedure
(denominator q)β(and/c integer? positive? )
1
4
1125899906842624.0
procedure
( rationalize xtolerance)βreal?
x:real?tolerance:real?
1/3
-1/3
0
1/2
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(integer-sqrt n)βcomplex?
n:integer?
2.0
2
0.0+2.0i
0+2i
procedure
n:integer?
2.0
0.0
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 other than exact 1 or 0, 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).
w is exact 0 β result is 1
w is 0.0 or -0.0 β result is 1.0
real part of w is negative β the exn:fail:contract:divide-by-zero exception is raised
w is nonreal with a nonpositive real part β the exn:fail:contract:divide-by-zero exception is raised
w is +nan.0 β result is +nan.0
otherwise β result is 0
w is negative β result is +inf.0
w is positive β result is 0.0
w is an odd integer β result is -inf.0
w otherwise rational β result is +inf.0
w is an odd integer β result is -0.0
w otherwise rational β result is 0.0
z is less than 1.0 β result is +inf.0
z is greater than 1.0 β result is 0.0
z is less than 1.0 β result is 0.0
z is greater than 1.0 β result is +inf.0
w is odd β result is -0.0
w is even β result is 0.0
w is odd β result is -inf.0
w is even β result is +inf.0
w is negative β result is 0.0
w is positive β result is +inf.0
If b is provided, it serves as an alternative base. It is equivalent to (/ (log z)(log b)), but can potentially run faster. If b is exact 1, exn:fail:contract:divide-by-zero exception is raised.
Consider using fllogb from math/flonum instead when accuracy is important.
1.0
1.2824746787307684+0.982793723247329i
0
2.0
3.0
1.0
Changed in version 6.9.0.1 of package base: Added second argument for arbitrary bases.
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 a 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.
0.4636476090008061
1.1071487177940904
-2.0344439357957027
1.530881333938778+0.19442614214700213i
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 0 for any positive x (not just exact values) when y is 0.
procedure
(make-rectangular xy)βnumber?
x:real?y:real?
3.0+4.0i
procedure
(make-polar magnitudeangle)βnumber?
magnitude:real?angle:real?
6.123233995736766e-16+10.0i
7.0710678118654755+7.071067811865475i
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 -inf.0 component.
The result is guaranteed to be between (- pi ) and pi , possibly equal to pi (but never equal to (- pi )).
3.141592653589793
0
0.9272952180016122
0.7853981633974483
3.141592653589793
procedure
(bitwise-ior n...)βexact-integer?
3
-31
procedure
(bitwise-and n...)βexact-integer?
0
-32
procedure
(bitwise-xor n...)βexact-integer?
4
31
procedure
(bitwise-not n)βexact-integer?
-6
0
procedure
(bitwise-bit-set? nm)βboolean?
This operation is equivalent to (not (zero? (bitwise-and n(arithmetic-shift 1m)))), but it is faster and runs in constant time when n is positive.
#t
#t
#t
procedure
(bitwise-bit-field nstartend)βexact-integer?
This operation is equivalent to the computation
but it runs in constant time when n is positive, start and end are fixnums, and (- endstart) 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.
"0"
0
"10"
2
"110"
6
procedure
(arithmetic-shift nm)βexact-integer?
1024
31
procedure
(integer-length n)βexact-integer?
4
3
procedure
( random k[rand-gen])βexact-nonnegative-integer?
rand-gen : pseudo-random-generator?min:exact-integer?rand-gen : pseudo-random-generator?rand-gen : 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 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 k)βvoid?
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?
rand-gen:pseudo-random-generator?
procedure
(pseudo-random-generator->vector rand-gen)
rand-gen:pseudo-random-generator?
procedure
procedure
rand-gen:pseudo-random-generator?
procedure
v:any/c
procedure
( crypto-random-bytes n)βbytes?
Returns n random bytes. On Unix systems, the bytes are obtained from "/dev/urandom", while Windows uses the RtlGenRand system function.
#"0ε1ε1ε2ε3ε5ε\b\r25ε\"7Y220ε351ε"
Added in version 6.3 of package base.
procedure
( random-ref seq[rand-gen])βany/c
seq:sequence?rand-gen : pseudo-random-generator?
Added in version 6.4 of package base.
procedure
n[ rand-genseq:sequence?rand-gen : pseudo-random-generator?
Like sequence-length , does not terminate on infinite sequences, and evaluates the entire sequence.
Added in version 6.4 of package base.
procedure
(number->string z[radix])βstring?
z:number?
"3.0"
"377"
procedure
[ radixconvert-modedecimal-modesingle-mode])s:string?= 'number-or-false
If convert-mode is 'number-or-false, the result is #f if s 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 s 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 .
3.0+2.5i
#f
57
7
"no exact representation for +inf.0"
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 n[decimal-digits])βstring?
Before printing, n is converted to an exact number, multiplied by (expt 10decimal-digits), rounded, and then divided again by (expt 10decimal-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 n 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 n must be rational? , despite the name of the function.)
"3.14"
"3.14159"
Changed in version 6.10.0.1 of package base: Added support for decoding a 1-byte string.
procedure
size-nsigned?[ big-endian?dest-bstrsigned?:any/c
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 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
[ big-endian?startbstr:bytes?
procedure
size-n[ big-endian?dest-bstrx:real?
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
value
Changed in version 7.3.0.5 of package base: Allow value to be a double-precision flonum.
procedure
( degrees->radians x)βreal?
x:real?
3.141592653589793
0.7071067811865475
procedure
( radians->degrees x)βreal?
x:real?
180.0
45.0
1
-1.0
0
-0.0
0.0
+nan.0
1.0
-1.0
procedure
( exact-round x)βexact-integer?
procedure
( exact-floor x)βexact-integer?
procedure
( exact-ceiling x)βexact-integer?
procedure
( exact-truncate x)βexact-integer?
2
3
-2
-3
procedure
( positive-integer? x)βboolean?
x:any/c
Added in version 6.8.0.2 of package base.
procedure
( negative-integer? x)βboolean?
x:any/c
Added in version 6.8.0.2 of package base.
procedure
( nonpositive-integer? x)βboolean?
x:any/c
Added in version 6.8.0.2 of package base.
procedure
( nonnegative-integer? x)βboolean?
x:any/c
Added in version 6.8.0.2 of package base.
Added in version 6.8.0.2 of package base.