C mathematical operations are a group of functions in the
standard library
In computer programming, a standard library is the library (computing), library made available across Programming language implementation, implementations of a programming language. Often, a standard library is specified by its associated program ...
of the
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
implementing basic mathematical functions.
Different C standards provide different, albeit backwards-compatible, sets of functions. Most of these functions are also available in the
C++ standard library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the origina ...
, though in different headers (the C headers are included as well, but only as a deprecated compatibility feature).
Overview of functions
Most of the mathematical functions, which use
floating-point numbers
In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a signed sequence of a fixed number of digits in some base) multiplied by an integer power of that base.
Numbers of this form ...
, are defined in
(
header in C++). The functions that operate on
integers
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
, such as
abs
,
labs
,
div
, and
ldiv
, are instead defined in the
header (
header in C++).
Any functions that operate on angles use
radian
The radian, denoted by the symbol rad, is the unit of angle in the International System of Units (SI) and is the standard unit of angular measure used in many areas of mathematics. It is defined such that one radian is the angle subtended at ...
s as the unit of angle.
Not all of these functions are available in the
C89 version of the standard. For those that are, the functions accept only type
double
for the floating-point arguments, leading to expensive type conversions in code that otherwise used single-precision
float
values. In C99, this shortcoming was fixed by introducing new sets of functions that work on
float
and
long double
arguments. Those functions are identified by
f
and
l
suffixes respectively.
Floating-point environment
C99
C99 (previously C9X, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps implementations mak ...
adds several functions and types for fine-grained control of floating-point environment.
These functions can be used to control a variety of settings that affect floating-point computations, for example, the rounding mode, on what conditions exceptions occur, when numbers are flushed to zero, etc. The floating-point environment functions and types are defined in
header (
in
C++).
Complex numbers
C99
C99 (previously C9X, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps implementations mak ...
adds a new
_Complex
keyword (and
complex
convenience macro; only available if the
header is included) that provides support for complex numbers. Any floating-point type can be modified with
complex
, and is then defined as a pair of floating-point numbers. Note that C99 and C++ do not implement complex numbers in a code-compatible way – the latter instead provides the class .
All operations on complex numbers are defined in the
header. As with the real-valued functions, an
f
or
l
suffix denotes the
float complex
or
long double complex
variant of the function.
A few more complex functions are "reserved for future use in C99". Implementations are provided by open-source projects that are not part of the standard library.
Type-generic functions
The header
defines a type-generic macro for each mathematical function defined in
and
. This adds a limited support for
function overloading
In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
of the mathematical functions: the same function name can be used with different types of parameters; the actual function will be selected at compile time according to the types of the parameters.
Each type-generic macro that corresponds to a function that is defined for both real and complex numbers encapsulates a total of 6 different functions:
float
,
double
and
long double
, and their
complex
variants. The type-generic macros that correspond to a function that is defined for only real numbers encapsulates a total of 3 different functions:
float
,
double
and
long double
variants of the function.
The C++ language includes native support for function overloading and thus does not provide the
header even as a compatibility feature.
Random-number generation
The header
(
in C++) defines several functions that can be used for statistically random number generation.
The
arc4random
family of random number functions are not defined in POSIX standard, but is found in some common
libc
implementations. It used to refer to the keystream generator of a leaked version of
RC4
In cryptography, RC4 (Rivest Cipher 4, also known as ARC4 or ARCFOUR, meaning Alleged RC4, see below) is a stream cipher. While it is remarkable for its simplicity and speed in software, multiple vulnerabilities have been discovered in RC4, ren ...
cipher (hence "alleged RC4"), but different algorithms, usually from other ciphers like
ChaCha20
Salsa20 and the closely related ChaCha are stream ciphers developed by Daniel J. Bernstein. Salsa20, the original cipher, was designed in 2005, then later submitted to the eSTREAM European Union cryptographic validation process by Bernstein. Ch ...
, have been implemented since using the same name.
The quality of randomness from
rand
are usually too weak to be even considered statistically random, and it requires explicit seeding. It is usually advised to use
arc4random
instead of
rand
when possible. Some C libraries implement
rand
using
arc4random_uniform
internally.
Implementations
Under
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
systems like
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
and
BSD
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginni ...
, the mathematical functions (as declared in
) are bundled separately in the mathematical library
. Therefore, if any of those functions are used, the linker must be given the directive
-lm
. There are various
libm
implementations, including:
*
GNU libc'
libm*
AMD
Advanced Micro Devices, Inc. (AMD) is an American multinational corporation and technology company headquartered in Santa Clara, California and maintains significant operations in Austin, Texas. AMD is a hardware and fabless company that de ...
'
libmgithub used almost as is by Windows
*
Intel C++ Compiler
Intel oneAPI DPC++/C++ Compiler and Intel C++ Compiler Classic (deprecated icc and icl is in Intel OneAPI HPC toolkit) are Intel’s C, C++, SYCL, and Data Parallel C++ (DPC++) compilers for Intel processor-based systems, available for Wind ...
libm
*
Red Hat
Red Hat, Inc. (formerly Red Hat Software, Inc.) is an American software company that provides open source software products to enterprises and is a subsidiary of IBM. Founded in 1993, Red Hat has its corporate headquarters in Raleigh, North ...
'
libm(Newlib)
*
Sun
The Sun is the star at the centre of the Solar System. It is a massive, nearly perfect sphere of hot plasma, heated to incandescence by nuclear fusion reactions in its core, radiating the energy from its surface mainly as visible light a ...
'
FDLIBM which was used as the basis for
FreeBSD
FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
'
msunand
OpenBSD
OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
'
libm both of which in turn were the basis of
Julia'
OpenLibm*
musl
musl is a C standard library intended for operating systems based on the Linux kernel, released under the MIT License. It was developed by Rich Felker to write a clean, efficient, and standards-conformant libc implementation.
Overview
musl wa ...
'
libm based on the BSD
libms
and other projects like Arm
*
LLVM
LLVM, also called LLVM Core, is a target-independent optimizer and code generator. It can be used to develop a Compiler#Front end, frontend for any programming language and a Compiler#Back end, backend for any instruction set architecture. LLVM i ...
's libm, which is correctly rounded (i.e. errors from the mathematically correct result are lower than 0.5
unit in the last place
In computer science and numerical analysis, unit in the last place or unit of least precision (ulp) is the spacing between two consecutive floating-point numbers, i.e., the value the '' least significant digit'' (rightmost digit) represents if it ...
)
* Arénaire project'
CRlibm(correctly rounded libm), and its successo
MetaLibm which uses
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 the ...
to automatically generate approximations that are formally proven.
* Rutger's RLIBM, which provides correctly rounded functions in single precision.
Implementations not necessarily under a name of include:
*
Arm
In human anatomy, the arm refers to the upper limb in common usage, although academically the term specifically means the upper arm between the glenohumeral joint (shoulder joint) and the elbow joint. The distal part of the upper limb between ...
's
* is a version of C/C++ math functions written for C++ (compile-time calculation)
CORE-MATH correctly rounded for single and double precision.
*
SIMD
Single instruction, multiple data (SIMD) is a type of parallel computer, parallel processing in Flynn's taxonomy. SIMD describes computers with multiple processing elements that perform the same operation on multiple data points simultaneousl ...
(vectorized) math libraries includ
SLEEFYeppp! and
Agner Fog's VCL, plus a few closed-source ones like SVML and DirectXMath.
See also
*
C99 floating-point support
References
External links
*
C reference for math functions
{{CProLang, state=expanded
C standard library