- C mathematical functions
-
C Standard Library - Data types
- Character classification
- Strings
- Mathematics
- File input/output
- Date/time
- Localization
- Memory allocation
- Program control
- Miscellaneous headers:
C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions.[1][2] Most of the functions involve the use of floating point numbers. Different C standards provide different albeit backwards-compatible, sets of functions. C mathematical functions are inherited in C++.
Contents
Overview of functions
Most of the mathematical functions are placed in
math.h
header (cmath
header in C++). The functions that operate on integers, such asabs
,labs
,div
, andldiv
, are instead specified in thestdlib.h
header (cstdlib
header in C++).Any functions that operate on angles use radians as the unit of angle.[1]
In C89, all functions accept only type
double
for the floating-point arguments. In C99, this limitation was fixed by introducing new sets of functions withf
andl
suffixes that work onfloat
andlong double
arguments respectively.[3],
,
- computes absolute value of an integral value
- computes absolute value of a floating point value
,
,
- computes the quotient and remainder of integer division
- remainder of the floating point division operation
- signed remainder of the division operation
- signed remainder as well as the three last bits of the division operation
- fused multiply-add operation
- larger of two floating point values
- smaller of two floating point values
- positive difference of two floating point values
,
,
- returns a not-a-number (NaN)
- Exponential functions
- returns e raised to the given power
- returns 2 raised to the given power
- returns e raised to the given power, minus one
- computes natural (base e) logarithm (to base e)
- computes common (base 10) logarithm
- computes natural logarithm (to base e) of 1 plus the given number
- extracts exponent of the number
- extracts exponent of the number
- Power functions
- computes square root
- computes cubic root
- computes square root of the sum of the squares of two given numbers
- raises a number to the given power
- Trigonometric functions
- computes sine
- computes cosine
- computes tangent
- computes arc sine
- computes arc cosine
- computes arc tangent
- computes arc tangent, using signs to determine quadrants
- Hyperbolic functions
- computes hyperbolic sine
- computes hyperbolic cosine
- computes hyperbolic tangent
- computes hyperbolic arc sine
- computes hyperbolic arc cosine
- computes hyperbolic arc tangent
- Error and gamma functions
- computes error function
- computes complementary error function
- computes natural logarithm of the gamma function
- computes gamma function
- Nearest integer floating point operations
- returns the nearest integer not less than the given value
- returns the nearest integer not greater than the given value
- returns the nearest integer not greater in magnitude than the given value
,
,
- returns the nearest integer, rounding away from zero in halfway cases
- returns the nearest integer using current rounding mode
,
,
- returns the nearest integer using current rounding mode with exception if the result differs
- Floating point manipulation functions
- decomposes a number into significand and a power of 2
- multiplies a number by 2 raised to a power
- decomposes a number into integer and fractional parts
,
- multiplies a number by FLT_RADIX raised to a power
,
- returns next representable floating point value towards the given value
- copies the sign of a floating point value
- Classification
- categorizes the given floating point value
- checks if the given number has finite value
- checks if the given number is infinite
- checks if the given number is NaN
- checks if the given number is normal
- checks if the given number is negative
Floating point environment
C99 adds several functions and types for fine-grained control of floating point computations.[3] The additional functions and types are defined in
fenv.h
header (cfenv
in C++).- clears exceptions
- stores current floating-point environment
- stores current status flags
- retrieves current rounding direction
- saves current floating-point environment and clears all exceptions
- raises a floating-point exceptions
- sets current floating-point environment
- sets current status flags
- sets current rounding direction
- tests whether certain exceptions have been raised
- restores floating-point environment, but keep current exceptions
- sets the precision mode
Complex numbers
C99 adds a new
_Complex
keyword that provides support for complex numbers. Any floating point type can be modified with_Complex
. In that case, a variable of such type contains a pair of floating point numbers and in such a way defines a complex number. C++ does not provide complex numbers in backwards compatible way. As an alternative, std::complex can be used.All operations on complex numbers are defined in
complex.h
header.- Basic operations
- computes absolute value
- computes argument of a complex number
- computes imaginary part of a complex number
- computes real part of a complex number
- computes complex conjugate
- computes complex projection into the Riemann sphere
- Exponentiation operations
- computes complex exponential
- computes complex logarithm
- computes complex square root
- computes complex power
- Trigonometric operations
- computes complex sine
- computes complex cosine
- computes complex tangent
- computes complex arc sine
- computes complex arc cosine
- computes complex arc tangent
- Hyperbolic operations
- computes complex hyperbolic sine
- computes complex hyperbolic cosine
- computes complex hyperbolic tangent
- computes complex hyperbolic arc sine
- computes complex hyperbolic arc cosine
- computes complex hyperbolic arc tangent
Type-generic functions
The header
tgmath.h
defines a type-generic macro for each mathematical function, so that the same function name can be used to call functions accepting different types of the arguments.Random number generation
The header
stdlib.h
(cstdlib
in C++) defines several functions that can be used for statistically random number generation[4]References
- ^ a b ISO/IEC 9899:1999 specification. p. 212, § 7.12. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf.
- ^ Prata, Stephen (2004). C primer plus. Sams Publishing. Appendix B, Section V: The Standard ANSI C Library with C99 Additions. ISBN 0-672-32696-5.
- ^ a b Prata, Stephen (2004). C primer plus. Sams Publishing. Appendix B, Section VIII: C99 Numeric Computational Enhancements. ISBN 0-672-32696-5.
- ^ "GNU C Library -- Mathematics". http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html. Retrieved 2 November 2011.
External links
- The Single UNIX® Specification, Issue 7 from The Open Group : mathematical declarations – Base Definitions Reference,
- Dinkumware math.h reference, a reference of all
math.h
functions - C++ reference for math functions inherited from C
Categories:
Wikimedia Foundation. 2010.