World Library  
Flag as Inappropriate
Email this Article

C mathematical functions

Article Id: WHEBN0004137504
Reproduction Date:

Title: C mathematical functions  
Author: World Heritage Encyclopedia
Language: English
Subject: C dynamic memory allocation, C alternative tokens, C standard library, C programming language, C++ programming language
Publisher: World Heritage Encyclopedia

C mathematical functions

C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions.[1][2] All functions use floating point 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 placed in math.h header (cmath header in C++). The functions that operate on integers, such as abs, labs, div, and ldiv, are instead specified in the stdlib.h header (cstdlib header in C++).

Any functions that operate on angles use radians as the unit of angle.[1]

Not all functions are available in C89 version of the C standard. For those that are, the functions accept only type double for the floating-point arguments. In C99, this limitation 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.[3]

Function Description
computes absolute value of an integer value
fabs computes absolute value of a floating point value
computes the quotient and remainder of integer division
fmod remainder of the floating point division operation
remainder signed remainder of the division operation
remquo signed remainder as well as the three last bits of the division operation
fma fused multiply-add operation
fmax larger of two floating point values
fmin smaller of two floating point values
fdim positive difference of two floating point values
returns a not-a-number (NaN)
exp returns e raised to the given power
exp2 returns 2 raised to the given power
expm1 returns e raised to the given power, minus one
log computes natural logarithm (to base e)
log2 computes binary logarithm (to base 2)
log10 computes common logarithm (to base 10)
log1p computes natural logarithm (to base e) of 1 plus the given number
ilogb extracts exponent of the number
logb extracts exponent of the number
sqrt computes square root
cbrt computes cubic root
hypot computes square root of the sum of the squares of two given numbers
pow raises a number to the given power
sin computes sine
cos computes cosine
tan computes tangent
asin computes arc sine
acos computes arc cosine
atan computes arc tangent
atan2 computes arc tangent, using signs to determine quadrants
sinh computes hyperbolic sine
cosh computes hyperbolic cosine
tanh computes hyperbolic tangent
asinh computes hyperbolic arc sine
acosh computes hyperbolic arc cosine
atanh computes hyperbolic arc tangent
Error and
erf computes error function
erfc computes complementary error function
lgamma computes natural logarithm of the gamma function
tgamma computes gamma function
ceil returns the nearest integer not less than the given value
floor returns the nearest integer not greater than the given value
trunc returns the nearest integer not greater in magnitude than the given value
returns the nearest integer, rounding away from zero in halfway cases
nearbyint returns the nearest integer using current rounding mode
returns the nearest integer using current rounding mode with exception if the result differs
frexp decomposes a number into significand and a power of 2
ldexp multiplies a number by 2 raised to a power
modf 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
copysign copies the sign of a floating point value
Classification fpclassify categorizes the given floating point value
isfinite checks if the given number has finite value
isinf checks if the given number is infinite
isnan checks if the given number is NaN
isnormal checks if the given number is normal
signbit checks if the given number is negative

Floating point environment

C99 adds several functions and types for fine-grained control of floating point environment.[3] 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 fenv.h header (cfenv in C++).

Function Description
feclearexcept clears exceptions (C99)
fegetenv stores current floating-point environment (C99)
fegetexceptflag stores current status flags (C99)
fegetround retrieves current rounding direction (C99)
feholdexcept saves current floating-point environment and clears all exceptions (C99)
feraiseexcept raises a floating-point exception (C99)
fesetenv sets current floating-point environment (C99)
fesetexceptflag sets current status flags (C99)
fesetround sets current rounding direction (C99)
fetestexcept tests whether certain exceptions have been raised (C99)
feupdateenv restores floating-point environment, but keeps current exceptions (C99)

Complex numbers

C99 adds a new _Complex keyword 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. C++ does not provide complex numbers in backwards compatible way, but provides the class

All operations on complex numbers are defined in complex.h header.

Function Description
cabs computes absolute value (C99)
carg computes argument of a complex number (C99)
cimag computes imaginary part of a complex number (C99)
creal computes real part of a complex number (C99)
computes complex conjugate (C99)
cproj computes complex projection into the Riemann sphere (C99)
cexp computes complex exponential (C99)
clog computes complex logarithm (C99)
csqrt computes complex square root (C99)
cpow computes complex power (C99)
csin computes complex sine (C99)
ccos computes complex cosine (C99)
ctan computes complex tangent (C99)
casin computes complex arc sine (C99)
cacos computes complex arc cosine (C99)
catan computes complex arc tangent (C99)
csinh computes complex hyperbolic sine (C99)
ccosh computes complex hyperbolic cosine (C99)
ctanh computes complex hyperbolic tangent (C99)
casinh computes complex hyperbolic arc sine (C99)
cacosh computes complex hyperbolic arc cosine (C99)
catanh computes complex hyperbolic arc tangent (C99)
A few more complex functions are "reserved for future use in C99".[4] Implementations are provided by open-source projects that are not part of the standard library.
Function Description
Error functions cerf computes the complex error function (C99)
cerfc computes the complex complementary error function (C99)

Type-generic functions

The header tgmath.h defines a type-generic macro for each mathematical function defined in math.h and complex.h. This adds a limited support for function overloading 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: two sets of
long double
variants. The type-generic macros that correspond to a function that is defined for only real numbers encapsulates a total of 3 different functions:
long double
variants of the function.

The C++ language includes native support for function overloading and thus does not provide the tgmath.h header even as a compatibility feature.

Random number generation

The header stdlib.h (cstdlib in C++) defines several functions that can be used for statistically random number generation[5]

Function Description
rand generates a pseudo-random number
srand initializes a pseudo-random number generator


Under Linux and FreeBSD, the mathematical functions (as declared in math.h) are bundled separately in the mathematical library libm. Therefore, if any of those functions are used, the linker must be given the directive -lm.

There are various libm implementations, including:

  • GNU libc's libm
  • Sun's FDLIBM, which was used as the basis for FreeBSD's msun, which in turn is the basis of Julia's OpenLibm
  • Arénaire project's CRlibm (Correctly Rounded mathematical library)
  • AMD's libm

See also


  1. ^ a b ISO/IEC 9899:1999 specification. p. 212, § 7.12. 
  2. ^ Prata, Stephen (2004). C primer plus. Sams Publishing. Appendix B, Section V: The Standard ANSI C Library with C99 Additions.  
  3. ^ a b Prata, Stephen (2004). C primer plus. Sams Publishing. Appendix B, Section VIII: C99 Numeric Computational Enhancements.  
  4. ^ man cerf(3), man cerfc(3), see e.g.
  5. ^ "GNU C Library -- Mathematics". Retrieved 2 November 2011. 

External links

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.