addaptic

Reference

Every public function in LZMath.h, one page each. The prototypes are taken from the header, and every example on these pages is what the engine printed.

What every function shares

Setup

Call lzu_setup once before the first calculation and lzu_takedown once after the last. Between them, any number of threads may compute at once.

Numbers

A CLZNumber is made from text, a long, a double or another number: CLZNumber x( "0.1" ). Text may name its base with a prefix, as "0x15" or "0b1010", and SetBase( 12 ) re-expresses a value in any base from 2 to 16. GetStringRep() gives the digits as text, which the caller releases with free(). A CLZComplex holds two numbers, m_real and m_imag, in one base.

Targets

Most functions take an optional target, the last parameter. Pass one and the answer lands there; leave it out and the answer replaces the left operand. A number marked constant is never written. A few functions require a target, because they have no operand of the right kind to write: the means of a list, the paired functions, and the complex functions whose answer is real.

Bases

Two operands need not share a base: the right one is brought into the left one's base on a copy, and never modified. Every answer comes back in the left operand's base, so the sine of a hexadecimal value is hexadecimal.

Precision and rounding

glWorkingPrecision sets how many places after the point an answer keeps, in its own base. It belongs to the thread, and starts at 36 in every new one. Every function rounds its answer correctly, half-even, however close the value comes to a rounding boundary. The four plain operations, lz_add, lz_subtract, lz_multiply and lz_divide, round in the target's own rounding mode instead, one of twelve. A small answer can keep its significant digits rather than rounding them away, and a quotient keeps a digit or two more; the examples on each page show what a function writes.

Angles

The circular functions and their inverses read or write angles in the thread's unit, geAngleUnit: AU_radians, the default, AU_degrees, AU_grads or AU_millicles. Outside radians an angle is reduced by whole turns exactly, so sin 30° is exactly 0.5 and tan 90° is a pole. The hyperbolic functions, the complex logarithm and the complex exponential always work in radians.

Errors and flags

A function answers kNoError (0) when it writes its answer. When there is no answer to give, it says so twice: it returns a code and flags the answer.

kFailed (1)An operand outside the function's domain, such as the square root of −9, or a required target missing.
kDivideByZero (3)A division by nought, or a pole such as tan 90°.
kUndefinedResult (4)No value at all, such as ln 0 or Γ(0).
kBaseMismatch (5)A complex value whose two parts are in different bases.
kTooLarge (6)An answer that would need more than 100,000 places, refused before any work is done. lzu_set_answer_limit changes the limit.

The flags are kfInfinity, kfQNaN and kfDiv0, read with IsInfinity() and FlagIsOn(). They carry through later arithmetic, so a failure early in a calculation is still visible at its end. Infinities carry no sign: infinity minus infinity is not-a-number. A comparison with a flagged number answers kUnordered.

Threads

Each thread keeps its own precision and angle unit. A number may be read by several threads at once but not written by one while another reads it, so give each thread its own results.

The functions

Setup

Three calls that bracket everything else. Call lzu_setup once before the first calculation, and lzu_takedown once after the last.

lzu_setupBuilds the shared table of high-precision constants; call it before anything else.
lzu_takedownReleases the shared table of constants; call it after the last calculation.
lzu_set_answer_limitSets how many places an answer may need before it is refused, for the whole process.

Arithmetic

The four operations, the whole quotient and its remainders, and the sign. These are the only functions that honour the target's own rounding mode.

lz_addThe sum of two numbers.
lz_subtractThe difference of two numbers, left minus right.
lz_multiplyThe product of two numbers.
lz_divideThe quotient of two numbers, left over right.
lz_divide_integerThe whole part of the quotient, truncated toward nought.
lz_moduloThe remainder after the truncated whole quotient; the same operation as lz_remainder.
lz_remainderThe remainder after the truncated whole quotient, left − trunc(left ÷ right) × right.
lz_remainder_nearThe remainder after the nearest whole quotient, ties to even.
lz_absThe absolute value.
lz_negateThe number with its sign changed.
lz_plusThe number itself, unchanged: the decimal standard's unary plus.
lz_reciprocalOne over the number.

Comparison

These four answer with a code rather than writing a number. Each answers kUnordered when either side is a not-a-number or an infinity, since an infinity here carries no sign.

lz_compareWhether the left number is less than, equal to or greater than the right.
lz_maxWhich of two numbers is the larger.
lz_minWhich of two numbers is the smaller.
lz_same_quantumWhether two numbers have the same number of places after the point.

Powers and roots

Whole powers are taken by repeated squaring, so their cost follows the number of digits in the exponent rather than its size. A root of whole order exactly on a half is decided exactly.

lz_squareThe number times itself.
lz_cubeThe number cubed.
lz_powerThe left number raised to the power of the right.
lz_raise_nThe left number raised to a whole power, the right.
lz_raise_ee raised to a whole power.
lz_root_2The square root.
lz_root_3The cube root.
lz_root_nThe root of the left number whose order is the right.

Exponentials

An answer too large to hold is refused before any work is done, and one that rounds to nought at the working precision is nought at once.

lz_exp_ee raised to the power of the number.
lz_exp_bThe number's own base raised to the power of the number.
lz_exp_nThe left number raised to the power of the right.

Logarithms

The logarithm of nought or of a negative number has no real answer, and is refused with a flag.

lz_log_eThe natural logarithm.
lz_log_bThe logarithm to the number's own base.
lz_log_nThe logarithm of the right number to the base of the left.

Trigonometry

All fourteen read or write their angle in the thread's angle unit: radians, degrees, grads or millicles, set in geAngleUnit. Outside radians an angle is reduced by whole turns exactly, so sin 30° is exactly 0.5 and tan 90° is a pole.

lz_sineThe sine of an angle.
lz_cosineThe cosine of an angle.
lz_tangentThe tangent of an angle.
lz_cotangentThe cotangent of an angle, one over its tangent.
lz_secantThe secant of an angle, one over its cosine.
lz_cosecantThe cosecant of an angle, one over its sine.
lz_sine_cosineThe sine and the cosine of one angle together, for about half the cost of the two.
lz_arcsineThe angle whose sine is the number.
lz_arccosineThe angle whose cosine is the number.
lz_arctangentThe angle whose tangent is the number.
lz_arccotangentThe angle whose cotangent is the number, arctan(1/x).
lz_arcsecantThe angle whose secant is the number.
lz_arccosecantThe angle whose cosecant is the number.
lz_arctangent2The angle of the point (x, y), with y first, as C's atan2 takes them.

Hyperbolic functions

Each forward function comes from a single ex. The hyperbolic functions take no angle unit: they are always in radians.

lz_hypersineThe hyperbolic sine.
lz_hypercosineThe hyperbolic cosine.
lz_hypertangentThe hyperbolic tangent.
lz_hypercotangentThe hyperbolic cotangent, one over the hyperbolic tangent.
lz_hypersecantThe hyperbolic secant, one over the hyperbolic cosine.
lz_hypercosecantThe hyperbolic cosecant, one over the hyperbolic sine.
lz_hypersine_hypercosineThe hyperbolic sine and cosine of one number together, from one exponential.
lz_archypersineThe number whose hyperbolic sine is the operand.
lz_archypercosineThe number at or above nought whose hyperbolic cosine is the operand.
lz_archypertangentThe number whose hyperbolic tangent is the operand.
lz_archypercotangentThe number whose hyperbolic cotangent is the operand.
lz_archypersecantThe number at or above nought whose hyperbolic secant is the operand.
lz_archypercosecantThe number whose hyperbolic cosecant is the operand.

Gamma

A whole number is answered exactly, as a factorial. Anything else above nought comes from Stirling's series after a shift, and below nought by reflection. The poles are refused.

lz_gammaThe gamma function, Γ(x).
lz_log_gammaThe natural logarithm of the gamma function, ln Γ(x), for x above nought.

Combinatorics

On the whole numbers, and exact. Choosing or arranging more than there are is nought.

lz_factorialThe product of the whole numbers from one to the operand, n!.
lz_combinationsThe number of ways to choose the right number of things from the left, n choose k.
lz_permutationsThe number of ordered arrangements of the right number of things from the left, n!/(n − k)!.

Means

The three means of a list are the only functions that take one: an array of numbers and a count. Having no left operand to update, they require a target.

lz_ameanThe arithmetic mean of a list.
lz_gmeanThe geometric mean of a list.
lz_hmeanThe harmonic mean of a list.
lz_agmThe arithmetic-geometric mean of two numbers that are not negative.

Decimal standard

Operations from the decimal arithmetic standards, on a number's places and its whole and fractional parts.

lz_quantizeThe left number rounded to as many places as the right number has.
lz_normalizeThe number with its trailing noughts after the point removed.
lz_roundThe number rounded in its own rounding mode, half-even by default.
lz_round_integralThe number rounded to a whole number, half-even by default.
lz_round_last_digitThe number rounded at its own last place.
lz_integerThe whole part of the number, truncated toward nought.
lz_fractionThe fractional part of the number, its sign kept.

Complex arithmetic

A CLZComplex holds two CLZNumbers, m_real and m_imag, in one base. Each part of an answer is correctly rounded from exact intermediates.

lzc_addThe sum of two complex numbers.
lzc_subtractThe difference of two complex numbers, left minus right.
lzc_negateThe complex number with both signs changed.
lzc_conjugateThe complex conjugate: the same real part, the imaginary part negated.
lzc_multiplyThe product of two complex numbers.
lzc_squareThe complex number times itself.
lzc_divideThe quotient of two complex numbers, left over right.
lzc_absThe modulus of a complex number, |z|, a real answer.
lzc_argThe argument of a complex number, its angle, a real answer in the thread's angle unit.
lzc_polarA complex number from its modulus and its angle, r cos θ + i r sin θ.

Complex exponentials, logarithms, powers and roots

The principal values, each part correctly rounded and exact where the mathematics is. The cut is the negative real axis, taken from the +0 side, as std::complex takes it.

lzc_exp_ee raised to a complex power.
lzc_log_eThe principal natural logarithm of a complex number.
lzc_root_2The principal square root of a complex number.
lzc_root_3The cube root of a complex number; an odd root of a negative real is the real root.
lzc_root_nThe principal root of complex order w, z^(1/w).
lzc_powerA complex number raised to a complex power, the principal value e^(w ln z).
lzc_raise_nA complex number raised to a whole power.
lzc_exp_bThe base the number is written in, raised to a complex power.
lzc_log_bThe principal logarithm to the base the number is written in.
lzc_log_nThe principal logarithm of the right complex number to the base of the left.

Complex circular and hyperbolic functions

The circular six read the whole complex angle in the thread's angle unit, so z degrees is zπ/180, both parts. The hyperbolic six are always in radians.

lzc_sineThe sine of a complex angle.
lzc_cosineThe cosine of a complex angle.
lzc_tangentThe tangent of a complex angle.
lzc_cotangentThe cotangent of a complex angle.
lzc_secantThe secant of a complex angle.
lzc_cosecantThe cosecant of a complex angle.
lzc_hypersineThe hyperbolic sine of a complex number.
lzc_hypercosineThe hyperbolic cosine of a complex number.
lzc_hypertangentThe hyperbolic tangent of a complex number.
lzc_hypercotangentThe hyperbolic cotangent of a complex number.
lzc_hypersecantThe hyperbolic secant of a complex number.
lzc_hypercosecantThe hyperbolic cosecant of a complex number.

Complex inverse functions

The principal values. On a branch cut each takes the operand's +0 side, as std::complex answers. The circular inverses write the whole complex angle in the thread's angle unit, the hyperbolic ones in radians.

lzc_arcsineThe principal arcsine of a complex number.
lzc_arccosineThe principal arccosine of a complex number.
lzc_arctangentThe principal arctangent of a complex number.
lzc_arccotangentThe principal arccotangent of a complex number, arctan(1/z).
lzc_arcsecantThe principal arcsecant of a complex number.
lzc_arccosecantThe principal arccosecant of a complex number.
lzc_archypersineThe principal inverse hyperbolic sine of a complex number.
lzc_archypercosineThe principal inverse hyperbolic cosine of a complex number.
lzc_archypertangentThe principal inverse hyperbolic tangent of a complex number.
lzc_archypercotangentThe principal inverse hyperbolic cotangent of a complex number.
lzc_archypersecantThe principal inverse hyperbolic secant of a complex number.
lzc_archypercosecantThe principal inverse hyperbolic cosecant of a complex number.