Bignum

An infinite-precision calculator and function library for Miranda

Input syntax for Miranda version

(The Haskell version just creates a new type BigFloat, to which all the usual operations apply)

Calculated constants

bn_0
The bignum version of zero
bn_1
The bignum version of one
bn_2
The bignum version of two
bn_e
2.7182818...
bn_pi
3.1415926535...
bn_pi_2, bn_pi_4 and bn_2_pi
pi/2, pi/4 and twice pi

Functions taking a single parameter

bn
takes a character string containing a decimal number and converts it to a bignum. The string can have an arbitrary number of decimal places after the decimal point.
ibn
takes an integer and converts it to a bignum (Note: an integer, not a floating point value, nor the result of a floating point calculation. You can try converting those using "bn (show x)", or more simply with the compound function "(bn.show), but entirely at your own risk!)
showbignum
is the inverse function of bn. It converts a bignum to a printable form. Miranda calls this function automatically whenever it has to display a bignum.
bn_twice
doubles its parameter (faster than "bn_times 2 x")
bn_half
halves its parameter (faster than "bn_quot x 2")
bn_sqr
returns the square of its argument (faster than "bn_mul x x")
bn_sqrt
returns the square root of its argument
bn_neg
negates its argument
bn_rnd
takes a positive integer seed as its parameter and returns a pseudo-random bignum in the range 0 <= n < 1

Functions taking two parameters

bn_add
takes two bignum parameters and returns their sum.
bn_sub
subtracts its second bignum parameter from its first, returning the difference.
bn_times
takes an integer and a bignum and returns their bignum product. The integer is the first of the two parameters, so as to ease partial parameterisazion.
bn_mul
multiplies two bignums
bn_quot
divides a bignum by an positive integer. If you want to partially parameterise the integer divisor, use "($bn_quot n)".
bn_div
performs long division of two bignums
bn_raise
takes a bignum and an integer and returns the bignum raised to that integral power
bn_pow
takes two bignums and returns the first raised to the power of the second

Trigonometric functions

All these take and return one bignum. All angles are expressed in radians.
bn_ln
returns the natural logarithm (in base e) of its parameter
bn_exp
returns the inverse natural logarithm of its parameter
bn_sin
returns the sine of its parameter. It is fastest for values between -pi/2 and +pi/2
bn_cos
returns the cosine of its parameter (defined using sin).
bn_tan
returns the tangent of its parameter (defined using sin/cos).
bn_asin
returns the arcsine (in the range -pi/2 to pi/2) of its parameter (defined using atan)
bn_acos
returns the arccosine (in the range -pi/2 to pi/2) of its parameter (defined using atan)
bn_atan
returns the arctangent (in the range -pi/2 to pi/2) of its parameter.
bn_sinh, bn_cosh, bn_tanh, bn_asinh, bn_acosh, bn_atanh
return hyperbolic sine, cosine and tangent and their inverses.

Logical functions

For the convenience of Miranda programmers who wish to use the bignum library as part of a larger program, there is also a set of comparison functions of little use in desk-calculator mode:
bn_is_zero
tells you whether a bignum is zero.
bn_is_neg
tells you whether a bignum has a negative value.
bn_cmp
compares two bignums and returns:
0 if the two numbers have the same value,
1 if the first number is greater than the second
-1 if the second number is greater than the first.
bn_eq, bn_ne, bn_gt, bn_lt, bn_ge, bn_le
The six usual comparison functions,
equal to, not equal to, greater than, less than,
greater than or equal to, less than or equal to.
They all take two bignum parameters and return a boolean value.

Examples

bn_sqrt bn_2
The square root of two.
bn_raise ( bn_mul (bn "1.5") (bn "0.75") ) 6
The interval obtained by tuning up by a perfect fifth and down by a perfect fourth, six times (not quite an octave!)
bn_quot (bn_add bn_1 (bn_sqrt (bn "5"))) 2
The golden ratio, calculated as (1 + sqrt(5)) / 2
Note that, since functions are "greedy" to bind to their parameters, brackets are necessary to make "5" the parameter to "bn", rather than "bn" being taken as the first parameter to "bn_sqrt" (which would make no sense).
Martin Guy <martinwguy@gmail.com>
Last modified: 2 January 2006