# Introduction to C programming/Simple math

C mathematical operations are functions in standard library of the C programming language implementing basic mathematical functions.All functions use numbers in one manner or another. Different C standards provide different sets of functions. Most of these functions are also available in the C++ standard library.

## Overview of functions

Most of the mathematical functions are defined in math.h (cmath header in C++). The functions that operate on integers, such as abs, labs, div, and ldiv, are instead defined in the stdlib.h header (cstdlib header in C++).
Any functions that operate on angles use radians as the unit of angle instead of using degree like in normal practise.
| | Function | Description |
| | abs labs llabs | computes absolute value of an integer value |
| | fabs | computes absolute value of a floating point value |
| | div ldiv lldiv | 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 |
| | nan nanf nanl | returns a not-a-number (NaN) |
| Exponential functions | exp | returns e raised to the given power |
| Exponential functions | exp2 | returns 2 raised to the given power |
| Exponential functions | expm1 | returns e raised to the given power, minus one |
| Exponential functions | log | computes natural algorithm (to base e) |
| Exponential functions | log2 | computes binary algorithm (to base 2) |
| Exponential functions | log10 | computes common algorithm (to base 10) |
| Exponential functions | log1p | computes natural logarithm (to base e) of 1 plus the given number |
| Exponential functions | ilogb | extracts exponent of the number |
| Exponential functions | logb | extracts exponent of the number |
| Power functions | sqrt | computes square root |
| Power functions | cbrt | computes cubic root |
| Power functions | hypot | computes square root of the sum of the squares of two given numbers |
| Power functions | pow | raises a number to the given power |
| Trigonometric functions | sin | computes sine |
| Trigonometric functions | cos | computes cosine |
| Trigonometric functions | tan | computes tangent |
| Trigonometric functions | asin | computes arc sine |
| Trigonometric functions | acos | computes arc cosine |
| Trigonometric functions | atan | computes arc tangent |
| Trigonometric functions | atan2 | computes arc tangent, using signs to determine quadrants |
| Hyperbolic functions | sinh | computes hyperbolic sine |
| Hyperbolic functions | cosh | computes hyperbolic cosine |
| Hyperbolic functions | tanh | computes hyperbolic tangent |
| Hyperbolic functions | asinh | computes hyperbolic arc sine |
| Hyperbolic functions | acosh | computes hyperbolic arc cosine |
| Hyperbolic functions | atanh | computes hyperbolic arc tangent |
| Error and gamma functions | erf | computes error function |
| Error and gamma functions | erfc | computes complementary error function |
| Error and gamma functions | lgamma | computes natural logarithm of the absolute value of the gamma language |
| Error and gamma functions | tgamma | computes gamma function |
| Nearest integer floating point operations | ceil | returns the nearest integer not less than the given value |
| Nearest integer floating point operations | floor | returns the nearest integer not greater than the given value |
| Nearest integer floating point operations | trunc | returns the nearest integer not greater in magnitude than the given value |
| Nearest integer floating point operations | round lround llround | returns the nearest integer, rounding away from zero in halfway cases |
| Nearest integer floating point operations | nearbyint | returns the nearest integer using current rounding mode |
| Nearest integer floating point operations | rint lrint llrint | returns the nearest integer using current rounding mode with exception if the result differs |
| Floating point manipulation functions | frexp | decomposes a number into significand and a power of 2 |
| Floating point manipulation functions | ldexp | multiplies a number by 2 raised to a power |
| Floating point manipulation functions | modf | decomposes a number into integer and fractional parts |
| Floating point manipulation functions | scalbn scalbln | multiplies a number by FLT_RADIX raised to a power |
| Floating point manipulation functions | nextafter nexttoward | returns next representable floating point value towards the given value |
| Floating point manipulation functions | copysign | copies the sign of a floating point value |
| Classification | fpclassify | categorizes the given floating point value |
| Classification | isfinite | checks if the given number has finite value |
| Classification | isinf | checks if the given number is infinite |
| Classification | isnan | checks if the given number is NaN |
| Classification | isnormal | checks if the given number is normal |
| Classification | signbit | checks if the given number is negative |

### Floating point environment

What is the FPU?
The FPU (Floating-Point Unit) is a unit within the CPU set up to perform math in floating point numbers. The reason this is done is, due to the different representations in memory, integral and floating-point numbers need a different physical operation to do math on them. An FPU should be able to addition, subtraction, multiplication and division (but not a modulo, due to problems with accuracy), among other operations, within a very small error margin.
Math with FPU
C99 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 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 (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 Template:Cpp.
All operations on complex numbers are defined in complex.h header. As with the real-valued functions, an f or l suffix denotes the float complex or long double complex variant of the function.
| | Function | Description |
| Basic operations | cabs | computes [[[w:Absolute value#Complex numbers|absolute value]] (C99) |
| Basic operations | carg | computes argument of a complex number (C99) |
| Basic operations | cimag | computes imaginary part of a complex number (C99) |
| Basic operations | creal | computes real part of a complex number (C99) |
| Basic operations | Template:Man | computes complex conjugate (C99) |
| Basic operations | cproj | computes complex projection into the Riemann sphere (C99) |
| Exponentiation operations | cexp | computes complex exponential (C99) |
| Exponentiation operations | clog | computes complex logarithm (C99) |
| Exponentiation operations | csqrt | computes complex square root (C99) |
| Exponentiation operations | cpow | computes complex power (C99) |
| Trigonometric operations | csin | computes complex sine (C99) |
| Trigonometric operations | ccos | computes complex cosine (C99) |
| Trigonometric operations | ctan | computes complex tangent (C99) |
| Trigonometric operations | casin | computes complex arc sine (C99) |
| Trigonometric operations | cacos | computes complex arc cosine (C99) |
| Trigonometric operations | catan | computes complex arc tangent (C99) |
| Hyperbolic operations | csinh | computes complex hyperbolic sine (C99) |
| Hyperbolic operations | ccosh | computes complex hyperbolic cosine (C99) |
| Hyperbolic operations | ctanh | computes complex hyperbolic tangent (C99) |
| Hyperbolic operations | casinh | computes complex hyperbolic arc sine (C99) |
| Hyperbolic operations | cacosh | computes complex hyperbolic arc cosine (C99) |
| Hyperbolic operations | catanh | computes complex hyperbolic arc tangent (C99) |
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.
| | Function | Description |
| Error functions | cerf | computes the complex error function (C99) |
| Error functions | 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: 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 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.
| Function | Description |
| rand | generates a pseudo-random number between 0 and RAND_MAX. |
| srand | initializes a pseudo-random number generator |
| arc4random | generates a pseudo-random number between 0 and UINT32_MAX, usually using a better algorithm than rand |
| arc4random_uniform | generates a pseudo-random number between 0 and a maximum value. |
| arc4random_buf | fill a buffer with a pseudo-random bitstream. |
| arc4random_stir | initializes a pseudo-random number generator. |
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 cipher (hence "alleged RC4"), but different algorithms, usually from other ciphers like ChaCha20, 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.