On this page:
1.1 Constants
phi.0
euler.0
gamma.0
catalan.0
1.2 Functions
float-complex?
number->float-complex
power-of-two?
asinh
acosh
atanh
sum
1.3 Random Number Generation
random-natural
random-integer
random-bits
1.4 Measuring Error
absolute-error
relative-error
8.1.0.6

1 Constants and Elementary Functions

 (require math/base) package: math-lib

For convenience, math/base re-exports racket/math as well as providing the values document below.

In general, the functions provided by math/base are elementary functions, or those functions that can be defined in terms of a finite number of arithmetic operations, logarithms, exponentials, trigonometric functions, and constants. For others, see math/special-functions and math/distributions.

1.1 Constants

If you need more accurate approximations than the following flonums, see, for example, phi.bf and bigfloat->rational.

An approximation of φ, the golden ratio.
> phi.0

1.618033988749895

An approximation of e, or Euler’s number.
> euler.0

2.718281828459045

> (exp 1)

2.718281828459045

An approximation of γ, the Euler-Mascheroni constant.
> gamma.0

0.5772156649015329

An approximation of G, or Catalan’s constant.
> catalan.0

0.915965594177219

1.2 Functions

procedure

(float-complex? v)  Boolean

  v : Any
Returns #t when v is of type Float-Complex. Analogous to flonum?.

procedure

(number->float-complex x)  Float-Complex

  x : Number
Returns a new complex number with a flonum real part and a flonum imaginary part. Analogous to real->double-flonum.

procedure

(power-of-two? x)  Boolean

  x : Real
Returns #t when x is an integer power of 2.

Examples:
> (power-of-two? 1.0)

#t

> (power-of-two? 1/2)

#t

> (power-of-two? (flnext 2.0))

#f

procedure

(asinh z)  Number

  z : Number

procedure

(acosh z)  Number

  z : Number

procedure

(atanh z)  Number

  z : Number
The inverses of sinh, cosh, and tanh, which are defined in racket/math (and re-exported by math/base).

procedure

(sum xs)  Real

  xs : (Listof Real)
Like (apply + xs), but incurs rounding error only once when adding inexact numbers. (In fact, the inexact numbers in xs are summed separately using flsum.)

1.3 Random Number Generation

procedure

(random-natural k)  Natural

  k : Integer
Returns a random natural number less than k, which must be positive. Use (random-natural k) instead of (random k) when k could be larger than 4294967087.

procedure

(random-integer a b)  Integer

  a : Integer
  b : Integer
Returns a random integer n such that (<= a n) and (< n b).

procedure

(random-bits num)  Natural

  num : Integer
Returns a random natural smaller than (expt 2 num); num must be positive. For powers of two, this is faster than using random-natural, which is implemented in terms of random-bits, using biased rejection sampling.

As an example of use, the significands of the numbers returned by bfrandom are chosen by (random-bits (bf-precision)).

1.4 Measuring Error

procedure

(absolute-error x r)  Real

  x : Real
  r : Real
Usually computes (abs (- x r)) using exact rationals, but handles non-rational reals such as +inf.0 specially.

Examples:
> (absolute-error 1/2 1/2)

0

> (absolute-error 0.14285714285714285 1/7)

7.93016446160826e-18

> (absolute-error +inf.0 +inf.0)

0.0

> (absolute-error +inf.0 +nan.0)

+inf.0

> (absolute-error 1e-20 0.0)

1e-20

> (absolute-error (- 1.0 (fl 4999999/5000000)) 1/5000000)

5.751132903242251e-18

procedure

(relative-error x r)  Real

  x : Real
  r : Real
Measures how close an approximation x is to the correct value r, relative to the magnitude of r.

This function usually computes (abs (/ (- x r) r)) using exact rationals, but handles non-rational reals such as +inf.0 specially, as well as r = 0.

Examples:
> (relative-error 1/2 1/2)

0

> (relative-error 0.14285714285714285 1/7)

5.551115123125783e-17

> (relative-error +inf.0 +inf.0)

0.0

> (relative-error +inf.0 +nan.0)

+inf.0

> (relative-error 1e-20 0.0)

+inf.0

> (relative-error (- 1.0 (fl 4999999/5000000)) 1/5000000)

2.8755664516211255e-11

In the last two examples, relative error is high because the result is near zero. (Compare the same examples with absolute-error.) Because flonums are particularly dense near zero, this makes relative error better than absolute error for measuring the error in a flonum approximation. An even better one is error in ulps; see flulp-error.