Base Math Library

Overview

The (base math) library contains procedures typically found in the Scheme R7RS inexact and complex libraries, as well as a handful of other numeric procedures toospecialized or esoteric for the core interpreter.

Inexact Procedures

This library provides a standard set of procedures for working with inexact (floating-point) real numbers. These procedures are essential for scientific and engineering applications that require trigonometry, logarithms, and exponentiation.

Cozenage, following the R7RS standard, represents special floating-point values such as positive infinity, negative infinity, and Not a Number as +inf.0, -inf.0, and +nan.0 respectively. This library includes predicates to test for these specific values.

(acos z)

Returns the arc cosine of z in radians.

Parameters:

z (number) – A number between -1 and 1, inclusive.

Returns:

The arc cosine in radians.

Return type:

inexact

Example:

--> (acos -1)
  3.141592653589793
--> (acos 0.5)
  1.0471975511965979
(asin z)

Returns the arc sine of z in radians.

Parameters:

z (number) – A number between -1 and 1, inclusive.

Returns:

The arc sine in radians.

Return type:

inexact

Example:

--> (asin 1)
  1.5707963267948966
--> (asin 0)
  0.0
(atan y)
(atan y x)

Returns the arc tangent of y/x in radians. If only one argument y is given, it returns the arc tangent of y.

Parameters:
  • y (real) – The numerator.

  • x (real) – The denominator.

Returns:

The arc tangent in radians.

Return type:

inexact

Example:

--> (atan 1)
  0.7853981633974483
--> (atan -1 0)
  -1.5707963267948966
(cos z)

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

Parameters:

z (number) – An angle in radians.

Returns:

The cosine of the angle.

Return type:

inexact

Example:

--> (cos 3.1415926535)
  -1.0
--> (cos 0)
  1.0
(exp z)

Returns e raised to the power of z, where e is the base of the natural logarithms.

Parameters:

z (number) – The exponent.

Returns:

e raised to the power of z.

Return type:

inexact

Example:

--> (exp 1)
  2.718281828459045
--> (exp 0)
  1.0
(finite? z)

Returns #true if z is a finite number, and #false otherwise. A number is finite if it is not positive or negative infinity and not NaN.

Parameters:

z (number) – The number to test.

Returns:

#true or #false.

Return type:

boolean

Example:

--> (finite? 123.45)
  #true
--> (finite? +inf.0)
  #false
--> (finite? +nan.0)
  #false
(infinite? z)

Returns #true if z is positive or negative infinity, and #false otherwise.

Parameters:

z (number) – The number to test.

Returns:

#true or #false.

Return type:

boolean

Example:

--> (infinite? (/ 1.0 0.0))
  #true
--> (infinite? -inf.0)
  #true
--> (infinite? 1000)
  #false
(log z)
(log z b)

Returns the natural logarithm of z. If the optional base b is provided, it returns the logarithm of z to the base b.

Parameters:
  • z (number) – The number.

  • b (number) – The base (optional).

Returns:

The logarithm of z.

Return type:

inexact

Example:

--> (log (exp 1))
  1.0
--> (log 100 10)
  2.0
(nan? z)

Returns #true if z is Not a Number (NaN), and #false otherwise.

Parameters:

z (number) – The number to test.

Returns:

#true or #false.

Return type:

boolean

Example:

--> (nan? (/ 0.0 0.0))
  #true
--> (nan? 123)
  #false
(sin z)

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

Parameters:

z (number) – An angle in radians.

Returns:

The sine of the angle.

Return type:

inexact

Example:

--> (sin 1.57079632679)
  1.0
--> (sin 0)
  0.0
(sqrt z)

Returns the principal square root of z.

Parameters:

z (number) – The number.

Returns:

The square root of z.

Return type:

number

Example:

--> (sqrt 16)
  4.0
--> (sqrt 2)
  1.4142135623730951
(tan z)

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

Parameters:

z (number) – An angle in radians.

Returns:

The tangent of the angle.

Return type:

inexact

Example:

--> (tan 0)
  0.0
--> (tan 0.785398)
  0.9999996208688432
(log2 z)

Returns the base-2 logarithm of z.

Parameters:

z (number) – The number.

Returns:

The base-2 logarithm of z.

Return type:

inexact

Example:

--> (log2 256)
  8.0
(log10 z)

Returns the base-10 logarithm of z.

Parameters:

z (number) – The number.

Returns:

The base-10 logarithm of z.

Return type:

inexact

Example:

--> (log10 1000)
  3.0
(cbrt z)

Returns the cube root of z.

Parameters:

z (number) – The number.

Returns:

The cube root of z.

Return type:

inexact

Example:

--> (cbrt 27)
  3.0
--> (cbrt -64)
  -4.0

Complex Number Procedures

The Cozenage complex number library provides a comprehensive set of procedures for creating and manipulating complex numbers. Complex numbers are a fundamental mathematical tool with wide-ranging applications in engineering, physics, signal processing, and graphics.

Cozenage supports two common representations of complex numbers:

  1. Rectangular Form: Represented as a+bi, where a is the real part and b is the imaginary part. This form is intuitive for addition and subtraction.

  2. Polar Form: Represented as r@θ, where r is the magnitude (or modulus) and θ is the angle (or argument). This form simplifies multiplication and division.

The library provides procedures to create complex numbers from both representations and to extract their component parts. All complex number objects are of the type complex.

(make-rectangular x1 x2)

Returns a complex number constructed from the real part x1 and the imaginary part x2.

Parameters:
  • x1 (real) – The real part.

  • x2 (real) – The imaginary part.

Returns:

The corresponding complex number.

Return type:

complex

Example:

--> (make-rectangular 3 4)
  3+4i
--> (make-rectangular -1.5 0)
  -1.5+0i
(real-part z)

Returns the real part of the complex number z.

Parameters:

z (complex) – The complex number to inspect.

Returns:

The real part of the number.

Return type:

real

Example:

--> (real-part 3+4i)
  3
--> (real-part -1.2-5.7i)
  -1.2
(imag-part z)

Returns the imaginary part of the complex number z.

Parameters:

z (complex) – The complex number to inspect.

Returns:

The imaginary part of the number.

Return type:

real

Example:

--> (imag-part 3+4i)
  4
--> (imag-part -1.2-5.7i)
  -5.7
(make-polar r theta)

Returns a complex number constructed from the magnitude r and the angle theta.

Parameters:
  • r (real) – The magnitude (radius).

  • theta (real) – The angle (in radians).

Returns:

The corresponding complex number.

Return type:

complex

Example:

--> (make-polar 5 0.927)
  3.00043+3.99974i
--> (make-polar 1 (atan 0 -1)) ; pi
  -1+1.22465e-16i
(magnitude z)

Returns the magnitude (or modulus) of the complex number z. This is the distance from the origin (0,0) to the point (real-part, imag-part) in the complex plane.

Parameters:

z (complex) – The complex number to inspect.

Returns:

The magnitude of the number.

Return type:

real

Example:

--> (magnitude 3+4i)
  5.0
--> (magnitude (make-polar 10 2))
  10.0
(angle z)

Returns the angle (or argument) of the complex number z in radians.

Parameters:

z (complex) – The complex number to inspect.

Returns:

The angle of the number in radians.

Return type:

real

Example:

--> (angle 3+4i)
  0.927295
--> (angle (make-polar 5 1.5))
  1.5