HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, a math library (or maths library) is a component of a
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
's standard library containing
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-orie ...
s (or
subroutine In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Functions ma ...
s) for the most common
mathematical function In mathematics, a function from a set to a set assigns to each element of exactly one element of .; the words map, mapping, transformation, correspondence, and operator are often used synonymously. The set is called the domain of the func ...
s, such as
trigonometry Trigonometry () is a branch of mathematics that studies relationships between side lengths and angles of triangles. The field emerged in the Hellenistic world during the 3rd century BC from applications of geometry to astronomical studies. ...
and
exponentiation Exponentiation is a mathematical operation, written as , involving two numbers, the '' base'' and the ''exponent'' or ''power'' , and pronounced as " (raised) to the (power of) ". When is a positive integer, exponentiation corresponds to re ...
. Bit-twiddling and control functionalities related to
floating point numbers In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be r ...
may also be included (such as in C). Examples include: * the C standard library math functions, * Java maths library * 'Prelude.Math' in haskell. In some languages (such as haskell) parts of the standard library (including maths) are imported by default. More advanced functionality such as
linear algebra Linear algebra is the branch of mathematics concerning linear equations such as: :a_1x_1+\cdots +a_nx_n=b, linear maps such as: :(x_1, \ldots, x_n) \mapsto a_1x_1+\cdots +a_nx_n, and their representations in vector spaces and through matric ...
is usually provided in 3rd party libraries, such as a
linear algebra library The following tables provide a comparison of linear algebra software libraries, either specialized or general purpose libraries with significant linear algebra coverage. Dense linear algebra General information Matrix types and operations ...
or
vector maths library This is a glossary of terms relating to computer graphics. For more general computer hardware terms, see glossary of computer hardware terms. 0–9 A B ...
.


Implementation outline


Basic operations

In a math library, it is frequently useful to use
type punning In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal ...
to interpret a floating-point number as an unsigned integer of the same size. This allows for faster inspection of certain numeral properties (positive or not) and number comparison. In more advanced cases,
bit twiddling Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, ...
may be used to modify a number in a specific way. For more exact operation, a ''double double'' or even ''triple double'' format may be used. In this case, a high-precision number is expressed as the sum of two or three floating-point numbers.


Transcendental functions

Transcendental function In mathematics, a transcendental function is an analytic function that does not satisfy a polynomial equation, in contrast to an algebraic function. In other words, a transcendental function "transcends" algebra in that it cannot be expressed alg ...
s such as log, exponential, and trig functions make up the backbone of any math library. These functions are generally implemented by a
polynomial fit In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable ''x'' and the dependent variable ''y'' is modelled as an ''n''th degree polynomial in ''x''. Polynomial regression f ...
, usually a
Taylor polynomial In mathematics, the Taylor series or Taylor expansion of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point. For most common functions, the function and the sum of its Taylor ser ...
or a
Chebyshev polynomial The Chebyshev polynomials are two sequences of polynomials related to the cosine and sine functions, notated as T_n(x) and U_n(x). They can be defined in several equivalent ways, one of which starts with trigonometric functions: The Chebyshe ...
derived by the
Remez algorithm The Remez algorithm or Remez exchange algorithm, published by Evgeny Yakovlevich Remez in 1934, is an iterative algorithm used to find simple approximations to functions, specifically, approximations by functions in a Chebyshev space that are th ...
(having the benefit of an improved error bound), but the pre-processing steps are equally important.


Trignometry

Range reduction (also ''argument reduction'', ''domain-spltting'') is the first step for any function, after checks for unusual values (infinity and
NaN Nan or NAN may refer to: Places China * Nan County, Yiyang, Hunan, China * Nan Commandery, historical commandery in Hubei, China Thailand * Nan Province ** Nan, Thailand, the administrative capital of Nan Province * Nan River People Given na ...
) are performed. The goal here is to reduce the domain of the argument for the polynomial to process, using the function's symmetry and perodicity (if any), setting flags to indicate e.g. whether to negate the result in the end (if needed). It is worth noting that periodic functions require higher-than-input precision when reducing, with the prototypical method being the Payne–Hanek–Corbett algorithm. After range reduction, near-zero values may be subject to a "fast path": for example, a tiny input can be returned directly in ''sin'', while may be used for ''cos''. The next step is the evaluation of the polynomial, with a conventional
Horner's method In mathematics and computer science, Horner's method (or Horner's scheme) is an algorithm for polynomial evaluation. Although named after William George Horner, this method is much older, as it has been attributed to Joseph-Louis Lagrange by Hor ...
used. After that, the sign of the result can be flipped according to the information from the range-reduction routine before being returned.


Logarithm and exponential

Logarithm in base 2 is relatively straightforward, as the integer part ''k'' is already in the floating-point exponent; a preliminary range reduction is accordingly performed, yielding ''k''. The mantissa ''x'' (where log2(''x'') is between -1/2 and 1/2) is then compared to a table and intervals for further reduction into a ''z'' with known log2 and an in-range ''x/z'', along with polynomial coefficients used for the interval ''x/z'' is in. The result is then log(''z'') + log(''x/z'') + ''k''.musl v1.2.2 math directory
''log1p.c'', ''log2.c'', ''log10.c'', ''exp2.c''
Logarithm in other bases follow a similar approach, with the differences being (a) a different table is used; (b) ''k'' needs to be multiplied by log2(''new-base''). Exponential in base 2 is similarly the "base case" due to floating-point structure. The procedure is simply a combination of range reduction (usually by lookup) and a polynomial over the remaining mantissa. The natural exponent may be implemented with a separate table for precision, while exp10 can simply be exp(''x'' × log2(10)) when in-range. Finally, the any-base exponent function pow() is built around exp() and log() in the general case.


See also

*
Intrinsic function In computer software, in compiler theory, an intrinsic function (or built-in function) is a function (subroutine) available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it may subst ...
*
List of numerical libraries This is a list of numerical libraries, which are libraries used in software development for performing numerical calculations. It is not a complete listing but is instead a list of numerical libraries with articles on Wikipedia, with few exceptio ...


References

{{Reflist Programming libraries