C mathematical operations are a group of functions in the
standard library
In computer programming, a standard library is the library made available across implementations of a programming language. These libraries are conventionally described in programming language specifications; however, contents of a language's as ...
of the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
implementing basic mathematical functions.
All functions use
floating-point
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 b ...
numbers in one manner or another. Different C standards provide different, albeit backwards-compatible, sets of functions. Most of these functions are also available in the
C++ standard library, 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 are defined in
(
header in C++). The functions that operate on integers, 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. The unit was formerly an SI supplementary unit (before that c ...
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 known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
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 known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
adds a new
_Complex
keyword (and
complex
convenience macro) 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
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 both the system- and user-level application programming interf ...
systems like
Linux
Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
and
BSD
The Berkeley Software Distribution or Berkeley Standard Distribution (BSD) is a discontinued operating system based on Research Unix, developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berk ...
, 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
The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s by t ...
'
libm*
AMD
Advanced Micro Devices, Inc. (AMD) is an American multinational semiconductor company based in Santa Clara, California, that develops computer processors and related technologies for business and consumer markets. While it initially manufactur ...
'
libm*
Red Hat
Red Hat, Inc. is an American software company that provides open source software products to enterprises. Founded in 1993, Red Hat has its corporate headquarters in Raleigh, North Carolina, with other offices worldwide.
Red Hat has become ass ...
'
libm*
Sun'
FDLIBM which was used as the basis for
FreeBSD
FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD), which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most popular ...
'
msunand
OpenBSD
OpenBSD is a security-focused, free and open-source, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by forking NetBSD 1.0. According to the website, the OpenBSD project em ...
'
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 with the goal to write a clean, efficient and standards-conformant libc implementation.
Ov ...
'
libm based on the BSD libms and other projects like ARM
* Arénaire project'
CRlibm(
correctly rounded libm), and its successo
MetaLibm Uses
Remez algorithm to automatically generate approximations that are formally proven.
Implementations not necessarily under a name of include:
*
ARM's
* is a version of C/C++ math functions written for C++ (compile-time calculation)
*
SIMD (vectorized) math libraries includ
SLEEFYeppp! and
Agner Fog
Agner Fog is a Danish evolutionary anthropologist and computer scientist. He is currently an Associate Professor of computer science at the Technical University of Denmark (DTU), and has been present at DTU since 1995. He is best known for coining ...
'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