4.3.2 Generic Numerics

4.3 Numbers
4.3.1 Number Types
4.3.3 Flonums
4.3.4 Fixnums
4.3.5 Extflonums
On this page:
4.3.2.1Arithmetic
+
-
*
/
abs
max
min
gcd
lcm
=
<
<=
>
>=
exp
log
sin
cos
tan
pi
sqr
sgn
top
up
4.3.2Generic NumericsπŸ”— i

Most Racket numeric operations work on any kind of number.

4.3.2.1ArithmeticπŸ”— i

procedure

(+ z...)β†’number?

Returns the sum of the zs, adding pairwise from left to right. If no arguments are provided, the result is 0.

Examples:
> (+ 12)

3

> (+ 1.02+3i5)

8.0+3.0i

> (+ )

0

procedure

(- z)β†’number?

(- zw...+)β†’number?
When no ws are supplied, returns (- 0z). Otherwise, returns the subtraction of the ws from z working pairwise from left to right.

Examples:
> (- 53.0)

2.0

> (- 1)

-1

> (- 2+7i13)

-2+7i

procedure

(* z...)β†’number?

Returns the product of the zs, multiplying pairwise from left to right. If no arguments are provided, the result is 1. Multiplying any number by exact 0 produces exact 0.

Examples:
> (* 23)

6

> (* 8.09)

72.0

> (* 1+2i3+4i)

-5+10i

procedure

( / z)β†’number?

(/ zw...+)β†’number?
When no ws are supplied, returns (/ 1z). Otherwise, returns the division of z by the ws working pairwise from left to right.

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:
> (/ 34)

3/4

> (/ 8133)

9

> (/ 10.0)

0.1

> (/ 1+2i3+4i)

11/25+2/25i

procedure

(quotient nm)β†’integer?

Returns (truncate (/ nm)).

Examples:
> (quotient 103)

3

> (quotient -10.03)

-3.0

> (quotient +inf.03)

quotient: contract violation

expected: integer?

given: +inf.0

procedure

(remainder nm)β†’integer?

Returns q with the same sign as n such that

  • (abs q) is between 0 (inclusive) and (abs m) (exclusive), and

  • (+ q(* m(quotient nm))) equals n.

If m is exact 0, the exn:fail:contract:divide-by-zero exception is raised.

Examples:
> (remainder 103)

1

> (remainder -10.03)

-1.0

> (remainder 10.0-3)

1.0

> (remainder -10-3)

-1

> (remainder +inf.03)

remainder: contract violation

expected: integer?

given: +inf.0

procedure

Returns (values (quotient nm)(remainder nm)), but the combination may be computed more efficiently than separate calls to quotient and remainder .

Example:

3

1

procedure

(modulo nm)β†’integer?

Returns q with the same sign as m where

  • (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.

Examples:
> (modulo 103)

1

> (modulo -10.03)

2.0

> (modulo 10.0-3)

-2.0

> (modulo -10-3)

-1

> (modulo +inf.03)

modulo: contract violation

expected: integer?

given: +inf.0

procedure

(add1 z)β†’number?

Returns (+ z1).

procedure

(sub1 z)β†’number?

Returns (- z1).

procedure

(abs x)β†’number?

x:real?
Returns the absolute value of x.

Examples:
> (abs 1.0)

1.0

> (abs -1)

1

procedure

(max x...+)β†’real?

x:real?
Returns the largest of the xs, or +nan.0 if any x is +nan.0. If any x is inexact, the result is coerced to inexact. See also argmax .

Examples:
> (max 132)

3

> (max 132.0)

3.0

procedure

(min x...+)β†’real?

x:real?
Returns the smallest of the xs, or +nan.0 if any x is +nan.0. If any x is inexact, the result is coerced to inexact. See also argmin .

Examples:
> (min 132)

1

> (min 132.0)

1.0

procedure

(gcd n...)β†’rational?

Returns the greatest common divisor (a non-negative number) of the ns; for non-integer ns, the result is the gcd of the numerators divided by the lcm of the denominators. If no arguments are provided, the result is 0. If all arguments are zero, the result is zero.

Examples:
> (gcd 10)

10

> (gcd 1281.0)

3.0

> (gcd 1/21/3)

1/6

procedure

(lcm n...)β†’rational?

Returns the least common multiple (a non-negative number) of the ns; non-integer ns, the result is the absolute value of the product divided by the gcd . If no arguments are provided, the result is 1. If any argument is zero, the result is zero; furthermore, if any argument is exact 0, the result is exact 0.

Examples:
> (lcm 10)

10

> (lcm 34.0)

12.0

> (lcm 1/22/3)

2

procedure

(round x)β†’(or/c integer? +inf.0-inf.0+nan.0)

x:real?
Returns the integer closest to x, resolving ties in favor of an even number, but +inf.0, -inf.0, and +nan.0 round to themselves.

Examples:
> (round 17/4)

4

> (round -17/4)

-4

> (round 2.5)

2.0

> (round -2.5)

-2.0

> (round +inf.0)

+inf.0

procedure

(floor x)β†’(or/c integer? +inf.0-inf.0+nan.0)

x:real?
Returns the largest integer that is no more than x, but +inf.0, -inf.0, and +nan.0 floor to themselves.

Examples:
> (floor 17/4)

4

> (floor -17/4)

-5

> (floor 2.5)

2.0

> (floor -2.5)

-3.0

> (floor +inf.0)

+inf.0

procedure

(ceiling x)β†’(or/c integer? +inf.0-inf.0+nan.0)

x:real?
Returns the smallest integer that is at least as large as x, but +inf.0, -inf.0, and +nan.0 ceiling to themselves.

Examples:
> (ceiling 17/4)

5

> (ceiling -17/4)

-4

> (ceiling 2.5)

3.0

> (ceiling -2.5)

-2.0

> (ceiling +inf.0)

+inf.0

procedure

(truncate x)β†’(or/c integer? +inf.0-inf.0+nan.0)

x:real?
Returns the integer farthest from 0 that is not farther from 0 than x, but +inf.0, -inf.0, and +nan.0 truncate to themselves.

Examples:
> (truncate 17/4)

4

> (truncate -17/4)

-4

> (truncate 2.5)

2.0

> (truncate -2.5)

-2.0

> (truncate +inf.0)

+inf.0

procedure

(numerator q)β†’integer?

Coerces q 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 q.

Examples:
> (numerator 5)

5

> (numerator 17/4)

17

> (numerator 2.3)

2589569785738035.0

procedure

(denominator q)β†’(and/c integer? positive? )

Coerces q 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 q.

Examples:

1

> (denominator 17/4)

4

> (denominator 2.3)

1125899906842624.0

procedure

( rationalize xtolerance)β†’real?

x:real?
tolerance:real?
Among the real numbers within (abs tolerance) of x, returns the one corresponding to an exact number whose denominator is the smallest. If multiple integers are within tolerance of x, the one closest to 0 is used.

Examples:
> (rationalize 1/41/10)

1/3

> (rationalize -1/41/10)

-1/3

> (rationalize 1/41/4)

0

> (rationalize 11/401/4)

1/2

4.3.2.2Number ComparisonπŸ”— i

procedure

(= zw...)β†’boolean?

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:
> (= 11.0)

#t

> (= 12)

#f

> (= 2+3i2+3i2+3i)

#t

> (= 1)

#t

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

procedure

(< xy...)β†’boolean?

x:real?
y:real?
Returns #t if the arguments in the given order are strictly increasing, #f otherwise.

Examples:
> (< 11)

#f

> (< 123)

#t

> (< 1)

#t

> (< 1+inf.0)

#t

> (< 1+nan.0)

#f

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

procedure

(<= xy...)β†’boolean?

x:real?
y:real?
Returns #t if the arguments in the given order are non-decreasing, #f otherwise.

Examples:
> (<= 11)

#t

> (<= 121)

#f

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

procedure

(> xy...)β†’boolean?

x:real?
y:real?
Returns #t if the arguments in the given order are strictly decreasing, #f otherwise.

Examples:
> (> 11)

#f

> (> 321)

#t

> (> +inf.01)

#t

> (> +nan.01)

#f

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

procedure

(>= xy...)β†’boolean?

x:real?
y:real?
Returns #t if the arguments in the given order are non-increasing, #f otherwise.

Examples:
> (>= 11)

#t

> (>= 121)

#f

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

4.3.2.3Powers and RootsπŸ”— i

procedure

(sqrt z)β†’number?

Returns the principal square root of z. The result is exact if z is exact and z’s square root is rational. See also integer-sqrt .

Examples:
> (sqrt 4/9)

2/3

> (sqrt 2)

1.4142135623730951

> (sqrt -1)

0+1i

procedure

(integer-sqrt n)β†’complex?

Returns (floor (sqrt n)) for positive n. The result is exact if n is exact. For negative n, the result is (* (integer-sqrt (- n))0+1i).

Examples:
> (integer-sqrt 4.0)

2.0

2

> (integer-sqrt -4.0)

0.0+2.0i

> (integer-sqrt -4)

0+2i

procedure

Returns (integer-sqrt n) and (- n(expt (integer-sqrt n)2)).

Examples:

2.0

0.0

2

1

procedure

(expt zw)β†’number?

Returns z raised to the power of w.

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).

If z is exact 0, the result is as follows:
  • 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

If w is exact 1/2, the result is the same as (sqrt z), which can be exact. Other fractional powers are not treated specially in this manner:

Examples:
> (expt 91/2)

3

> (expt 90.5)

3.0

> (expt 161/4)

2.0

> (expt 160.25)

2.0

Further special cases when w is a real number: These special cases correspond to pow in C99 [C99], except when z is negative and w is a not an integer.
  • (expt 0.0w):
    • w is negative β€” result is +inf.0

    • w is positive β€” result is 0.0

  • (expt -0.0w):
    • w is negative:
      • w is an odd integer β€” result is -inf.0

      • w otherwise rational β€” result is +inf.0

    • w is positive:
      • w is an odd integer β€” result is -0.0

      • w otherwise rational β€” result is 0.0

  • (expt z-inf.0) for positive z:
    • z is less than 1.0 β€” result is +inf.0

    • z is greater than 1.0 β€” result is 0.0

  • (expt z+inf.0) for positive z:
    • z is less than 1.0 β€” result is 0.0

    • z is greater than 1.0 β€” result is +inf.0

  • (expt -inf.0w) for integer w:
    • w is negative:
      • w is odd β€” result is -0.0

      • w is even β€” result is 0.0

    • w is positive:
      • w is odd β€” result is -inf.0

      • w is even β€” result is +inf.0

  • (expt +inf.0w):
    • w is negative β€” result is 0.0

    • w is positive β€” result is +inf.0

Examples:
> (expt 23)

8

> (expt 40.5)

2.0

> (expt +inf.00)

1

procedure

(exp z)β†’number?

Returns Euler’s number raised to the power of z. The result is normally inexact, but it is exact 1 when z is an exact 0. See also expt .

Examples:
> (exp 1)

2.718281828459045

> (exp 2+3i)

-7.315110094901103+1.0427436562359045i

> (exp 0)

1

procedure

(log z[b])β†’number?

b:number? =(exp 1)
Returns the natural logarithm of z. The result is normally inexact, but it is exact 0 when z is an exact 1. When z is exact 0, exn:fail:contract:divide-by-zero exception is raised.

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.

Examples:
> (log (exp 1))

1.0

> (log 2+3i)

1.2824746787307684+0.982793723247329i

> (log 1)

0

> (log 10010)

2.0

> (log 82)

3.0

> (log 55)

1.0

Changed in version 6.9.0.1 of package base: Added second argument for arbitrary bases.

4.3.2.4Trigonometric FunctionsπŸ”— i

procedure

(sin z)β†’number?

Returns the sine of z, where z is in radians. The result is normally inexact, but it is exact 0 if z is exact 0.

Examples:
> (sin 3.14159)

2.65358979335273e-6

> (sin 1.0+5.0i)

62.44551846769654+40.0921657779984i

procedure

(cos z)β†’number?

Returns the cosine of z, where z is in radians.

Examples:
> (cos 3.14159)

-0.9999999999964793

> (cos 1.0+5.0i)

40.09580630629883-62.43984868079963i

procedure

(tan z)β†’number?

Returns the tangent of z, where z is in radians. The result is normally inexact, but it is exact 0 if z is exact 0.

Examples:
> (tan 0.7854)

1.0000036732118496

> (tan 1.0+5.0i)

8.256719834229597e-5+1.0000377833796008i

procedure

(asin z)β†’number?

Returns the arcsine in radians of z. The result is normally inexact, but it is exact 0 if z is exact 0.

Examples:
> (asin 0.25)

0.25268025514207865

> (asin 1.0+5.0i)

0.1937931365549322+2.3309746530493123i

procedure

(acos z)β†’number?

Returns the arccosine in radians of z.

Examples:
> (acos 0.25)

1.318116071652818

> (acos 1.0+5.0i)

1.3770031902399644-2.3309746530493123i

procedure

(atan z)β†’number?

(atan yx)β†’number?
y:real?
x:real?
In the one-argument case, returns the arctangent of the inexact approximation of z, except that the result is an exact 0 for z as 0, and the exn:fail:contract:divide-by-zero exception is raised for z as exact 0+1i or exact 0-1i.

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.

Examples:
> (atan 0.5)

0.4636476090008061

> (atan 21)

1.1071487177940904

> (atan -2-1)

-2.0344439357957027

> (atan 1.0+5.0i)

1.530881333938778+0.19442614214700213i

> (atan +inf.0-inf.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 0 for any positive x (not just exact values) when y is 0.

4.3.2.5Complex NumbersπŸ”— i

procedure

(make-rectangular xy)β†’number?

x:real?
y:real?
Creates a complex number with x as the real part and y as the imaginary part. That is, returns (+ x(* y0+1i)).

Example:

3.0+4.0i

procedure

(make-polar magnitudeangle)β†’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 z)β†’real?

Returns the real part of the complex number z in rectangle coordinates.

Examples:
> (real-part 3+4i)

3

> (real-part 5.0)

5.0

procedure

(imag-part z)β†’real?

Returns the imaginary part of the complex number z in rectangle coordinates.

Examples:
> (imag-part 3+4i)

4

> (imag-part 5.0)

0

> (imag-part 5.0+0.0i)

0.0

procedure

(magnitude z)β†’(and/c real? (not/c negative? ))

Returns the magnitude of the complex number z in polar coordinates. A complex number with +inf.0 or -inf.0 as a component has magnitude +inf.0, even if the other component is +nan.0.

Examples:
> (magnitude -3)

3

> (magnitude 3.0)

3.0

> (magnitude 3+4i)

5

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.

procedure

(angle z)β†’real?

Returns the angle of the complex number z 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)

0

> (angle 3+4i)

0.9272952180016122

> (angle +inf.0+inf.0i)

0.7853981633974483

> (angle -1)

3.141592653589793

4.3.2.6Bitwise OperationsπŸ”— i

procedure

(bitwise-ior n...)β†’exact-integer?

Returns the bitwise β€œinclusive or” of the ns in their (semi-infinite) two’s complement representation. If no arguments are provided, the result is 0.

Examples:
> (bitwise-ior 12)

3

> (bitwise-ior -321)

-31

procedure

(bitwise-and n...)β†’exact-integer?

Returns the bitwise β€œand” of the ns in their (semi-infinite) two’s complement representation. If no arguments are provided, the result is -1.

Examples:
> (bitwise-and 12)

0

> (bitwise-and -32-1)

-32

procedure

(bitwise-xor n...)β†’exact-integer?

Returns the bitwise β€œexclusive or” of the ns in their (semi-infinite) two’s complement representation. If no arguments are provided, the result is 0.

Examples:
> (bitwise-xor 15)

4

> (bitwise-xor -32-1)

31

procedure

(bitwise-not n)β†’exact-integer?

Returns the bitwise β€œnot” of n in its (semi-infinite) two’s complement representation.

Examples:

-6

> (bitwise-not -1)

0

procedure

(bitwise-bit-set? nm)β†’boolean?

Returns #t when the mth bit of n is set in n’s (semi-infinite) two’s complement representation.

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.

Examples:

#t

#t

> (bitwise-bit-set? -5(expt 2700))

#t

procedure

(bitwise-bit-field nstartend)β†’exact-integer?

Extracts the bits between position start and (- end1) (inclusive) from n and shifts them down to the least significant portion of the number.

This operation is equivalent to the computation

(arithmetic-shift n(- start)))

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.

Examples:
> (format "~b"(bitwise-bit-field (string->number "1101"2)11))

"0"

0

> (format "~b"(bitwise-bit-field (string->number "1101"2)13))

"10"

2

> (format "~b"(bitwise-bit-field (string->number "1101"2)14))

"110"

6

procedure

(arithmetic-shift nm)β†’exact-integer?

Returns the bitwise β€œshift” of n in its (semi-infinite) two’s complement representation. If m is non-negative, the integer n is shifted left by m bits; i.e., m new zeros are introduced as rightmost digits. If m is negative, n is shifted right by (- m) bits; i.e., the rightmost m digits are dropped.

Examples:

1024

> (arithmetic-shift 255-3)

31

procedure

(integer-length n)β†’exact-integer?

Returns the number of bits in the (semi-infinite) two’s complement representation of n after removing all leading zeros (for non-negative n) or ones (for negative n).

Examples:

4

3

4.3.2.7Random NumbersπŸ”— i

procedure

( random k[rand-gen])β†’exact-nonnegative-integer?

k:(integer-in 14294967087)
(random minmax[rand-gen])β†’exact-integer?
max:(integer-in (+ 1min)(+ 4294967087min))
(random [rand-gen])β†’(and/c real? inexact? (>/c 0)(</c 1))
When called with an integer argument k, returns a random exact integer in the range 0 to k-1.

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?

k:(integer-in 0(sub1 (expt 231)))
Seeds the current pseudo-random number generator with k. 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 k. Use vector->pseudo-random-generator! to set a pseudo-random number generator to any of its possible states.

Returns a new pseudo-random number generator. The new generator is seeded with a number derived from (current-milliseconds ).

v:any/c
Returns #t if v is a pseudo-random number generator, #f otherwise.

A parameter that determines the pseudo-random number generator used by random .

procedure

(pseudo-random-generator->vector rand-gen)

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).

Produces a pseudo-random number generator whose internal state corresponds to vec.

procedure

vec) β†’ void?
Like vector->pseudo-random-generator , but changes rand-gen to the given state, instead of creating a new generator.

v:any/c
Returns #t if v is a vector of six exact integers, where the first three integers are in the range 0 to 4294967086, inclusive; the last three integers are in the range 0 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.8Other Randomness UtilitiesπŸ”— i

(require racket/random ) package: base

procedure

( crypto-random-bytes n)β†’bytes?

Provides an interface to randomness from the underlying operating system. Use crypto-random-bytes instead of random wherever security is a concern.

Returns n random bytes. On Unix systems, the bytes are obtained from "/dev/urandom", while Windows uses the RtlGenRand system function.

Example:

#"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?
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

n
[ rand-gen
#:replacement?replacement?]) β†’ (listof any/c )
seq:sequence?
replacement?:any/c =#t
Returns a list of n 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πŸ”— i

procedure

(number->string z[radix])β†’string?

radix:(or/c 281016)=10
Returns a string that is the printed form of z (see Printing Numbers) in the base specified by radix. If z is inexact, radix must be 10, otherwise the exn:fail:contract exception is raised.

Examples:

"3.0"

> (number->string 2558)

"377"

procedure

[ radix
convert-mode
decimal-mode
single-mode])
β†’(or/c number? #fstring? extflonum?)
radix:(integer-in 216)=10
convert-mode : (or/c 'number-or-false'read)
= 'number-or-false
decimal-mode : (or/c 'decimal-as-inexact'decimal-as-exact)
=
'decimal-as-inexact
'decimal-as-exact)
single-mode : (or/c 'single'double)
=
'single
'double)
Reads and returns a number datum from s (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 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 .

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

> (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 n[decimal-digits])β†’string?

decimal-digits:exact-nonnegative-integer? =2
Prints n into a string and returns the string. The printed form of n shows exactly decimal-digits digits after the decimal point. The printed form uses a minus sign if n is negative, and it does not use a plus sign if n is positive.

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.)

Examples:

"3.14"

"3.14159"

procedure

signed?
[ big-endian?
start
end]) β†’ exact-integer?
bstr:bytes?
signed?:any/c
big-endian?:any/c =(system-big-endian? )
Converts the machine-format number encoded in bstr to an exact integer. The start and end arguments specify the substring to decode, where (- endstart) must be 1, 2, 4, or 8. 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

size-n
signed?
[ big-endian?
dest-bstr
start]) β†’ bytes?
size-n:(or/c 1248)
signed?:any/c
big-endian?:any/c =(system-big-endian? )
dest-bstr : (and/c bytes? (not/c immutable? ))
= (make-bytes size-n)
Converts the exact integer n to a machine-format number encoded in a byte string of length size-n, which must be 1, 2, 4, or 8. 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 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?
start
end]) β†’ flonum?
bstr:bytes?
big-endian?:any/c =(system-big-endian? )
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

size-n
[ big-endian?
dest-bstr
start]) β†’ bytes?
x:real?
size-n:(or/c 48)
big-endian?:any/c =(system-big-endian? )
dest-bstr : (and/c bytes? (not/c immutable? ))
= (make-bytes size-n)
Converts the real number x to its IEEE representation in a byte string of length size-n, which must be 4 or 8. 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 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

(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.10Extra Constants and FunctionsπŸ”— i

(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

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 x)β†’real?

x:real?
Converts an x-degree angle to radians.

Examples:

3.141592653589793

0.7071067811865475

procedure

( radians->degrees x)β†’real?

x:real?
Converts x radians to degrees.

Examples:

180.0

> (radians->degrees (* 1/4pi ))

45.0

procedure

( sqr z)β†’number?

Returns (* zz).

procedure

( sgn x)β†’(or/c (=/c -1)(=/c 0)(=/c 1)+nan.0+nan.f)

x:real?
Returns the sign of x as either -1, 0 (or a signed-zero variant, when inexact), 1, or not-a-number.

Examples:
> (sgn 10)

1

> (sgn -10.0)

-1.0

> (sgn 0)

0

> (sgn -0.0)

-0.0

> (sgn 0.0)

0.0

> (sgn +nan.0)

+nan.0

> (sgn +inf.0)

1.0

> (sgn -inf.0)

-1.0

procedure

( conjugate z)β†’number?

Returns the complex conjugate of z.

Examples:
> (conjugate 1)

1

> (conjugate 3+4i)

3-4i

procedure

( sinh z)β†’number?

Returns the hyperbolic sine of z.

procedure

( cosh z)β†’number?

Returns the hyperbolic cosine of z.

procedure

( tanh z)β†’number?

Returns the hyperbolic tangent of z.

procedure

( exact-round x)β†’exact-integer?

Equivalent to (inexact->exact (round x)).

procedure

( exact-floor x)β†’exact-integer?

Equivalent to (inexact->exact (floor x)).

procedure

( exact-ceiling x)β†’exact-integer?

Equivalent to (inexact->exact (ceiling x)).

procedure

( exact-truncate x)β†’exact-integer?

Equivalent to (inexact->exact (truncate x)).

procedure

( order-of-magnitude r)β†’(and/c exact? integer? )

Computes the greatest exact integer m such that:
(<= (expt 10m)
Hence also:
(expt 10(add1 m)))

Examples:

2

3

-2

-3

procedure

( nan? x)β†’boolean?

x:real?
Returns #t if x is eqv? to +nan.0 or +nan.f; otherwise #f.

procedure

( infinite? x)β†’boolean?

x:real?
Returns #t if x is +inf.0, -inf.0, +inf.f, -inf.f; otherwise #f.

procedure

( positive-integer? x)β†’boolean?

x: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? x)β†’boolean?

x:any/c
The same as (and (integer? x)(negative? x)).

Added in version 6.8.0.2 of package base.

procedure

( nonpositive-integer? x)β†’boolean?

x:any/c
The same as (and (integer? x)(not (positive? x))).

Added in version 6.8.0.2 of package base.

procedure

( nonnegative-integer? x)β†’boolean?

x: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? x)β†’boolean?

x:any/c
An alias for exact-nonnegative-integer? .

Added in version 6.8.0.2 of package base.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /