YouTip LogoYouTip

C Standard Library Math H

## Introduction The **math.h** header file defines various mathematical functions and one macro. All functions available in this library take a **double** type argument and return a **double** type result. `` is a header file in the C standard library that contains numerous functions and macros for mathematical computations. These functions and macros provide basic arithmetic operations, trigonometric functions, exponential functions, logarithmic functions, power functions, rounding functions, and more. ## Library Macros In ``, several macros are used to represent error conditions for mathematical functions: | Macro | Description | | --- | --- | | `HUGE_VAL` | Value returned when a function result overflows (positive infinity). This macro represents a very large double-precision floating-point number, typically used as the return value of certain mathematical functions when the result exceeds the representable range. When a function's result is too large to be represented as a normal floating-point number (i.e., an overflow occurs), `errno` is set to `ERANGE` (range error) and the function returns `HUGE_VAL` or its negative value (for negative infinity). | | `HUGE_VALF` | Value returned when a function result overflows (positive infinity, float type) | | `HUGE_VALL` | Value returned when a function result overflows (positive infinity, long double type) | | `INFINITY` | Positive infinity | | `NAN` | Not-a-Number value | | `FP_INFINITE` | Represents infinity | | `FP_NAN` | Represents not-a-number | | `FP_NORMAL` | Represents a normal floating-point number | | `FP_SUBNORMAL` | Represents a subnormal number | | `FP_ZERO` | Represents zero | ## Library Functions The following lists the functions defined in the math.h header file: | No. | Function & Description | | --- | --- | | 1 | [double acos(double x)](#) Returns the arc cosine of x in radians. | | 2 | [double asin(double x)](#) Returns the arc sine of x in radians. | | 3 | [double atan(double x)](#) Returns the arc tangent of x in radians. | | 4 | [double atan2(double y, double x)](#) Returns the arc tangent in radians of y/x. The signs of y and x determine the correct quadrant. | | 5 | [double cos(double x)](#) Returns the cosine of a radian angle x. | | 6 | [double cosh(double x)](#) Returns the hyperbolic cosine of x. | | 7 | [double sin(double x)](#) Returns the sine of a radian angle x. | | 8 | [double sinh(double x)](#) Returns the hyperbolic sine of x. | | 9 | [double tanh(double x)](#) Returns the hyperbolic tangent of x. | | 10 | [double exp(double x)](#) Returns the value of e raised to the power of x. | | 11 | [double frexp(double x, int *exponent)](#) Breaks down the floating-point number x into a mantissa and an exponent. Returns the mantissa and stores the exponent in *exponent. The result satisfies x = mantissa * 2 ^ exponent. | | 12 | [double ldexp(double x, int exponent)](#) Returns x multiplied by 2 raised to the power exponent. | | 13 | [double log(double x)](#) Returns the natural logarithm of x (base-e logarithm). | | 14 | [double log10(double x)](#) Returns the common logarithm of x (base-10 logarithm). | | 15 | [double modf(double x, double *integer)](#) Returns the fractional part of x and stores the integer part in *integer. | | 16 | [double pow(double x, double y)](#) Returns x raised to the power y. | | 17 | [double sqrt(double x)](#) Returns the square root of x. | | 18 | [double ceil(double x)](#) Returns the smallest integer value greater than or equal to x. | | 19 | [double fabs(double x)](#) Returns the absolute value of x. | | 20 | [double floor(double x)](#) Returns the largest integer value less than or equal to x. | | 21 | [double fmod(double x, double y)](#) Returns the remainder of x divided by y. | ### Common Mathematical Constants The following are some commonly used mathematical constants defined in ``: | Constant | Value | Description | | --- | --- | --- | | `M_PI` | 3.14159265358979323846 | Pi (Ο€) | | `M_E` | 2.71828182845904523536 | Base of natural logarithms (e) | | `M_LOG2E` | 1.44269504088896340736 | logβ‚‚(e) | | `M_LOG10E` | 0.43429448190325182765 | log₁₀(e) | | `M_LN2` | 0.69314718055994530942 | ln(2) | | `M_LN10` | 2.30258509299404568402 | ln(10) | | `M_PI_2` | 1.57079632679489661923 | Ο€/2 | | `M_PI_4` | 0.78539816339744830962 | Ο€/4 | | `M_1_PI` | 0.31830988618379067154 | 1/Ο€ | | `M_2_PI` | 0.63661977236758134308 | 2/Ο€ | | `M_2_SQRTPI` | 1.12837916709551257390 | 2/βˆšΟ€ | | `M_SQRT2` | 1.41421356237309504880 | √2 | | `M_SQRT1_2` | 0.70710678118654752440 | 1/√2 | Below is sample code demonstrating the use of functions and constants from ``: ## Example #include #include int main(){ double x =2.0; double y =3.0; // Basic operations printf("sqrt(%.1f) = %.2fn", x,sqrt(x)); printf("pow(%.1f, %.1f) = %.2fn", x, y,pow(x, y)); printf("exp(%.1f) = %.2fn", x,exp(x)); printf("log(%.1f) = %.2fn", x,log(x)); printf("log10(%.1f) = %.2fn", x,log10(x)); // Trigonometric functions double angle = M_PI /4;// 45 degrees printf("sin(%.2f) = %.2fn", angle,sin(angle)); printf("cos(%.2f) = %.2fn", angle,cos(angle)); printf("tan(%.2f) = %.2fn", angle,tan(angle)); // Rounding functions double z =5.8; printf("ceil(%.1f) = %.1fn", z,ceil(z)); printf("floor(%.1f) = %.1fn", z,floor(z)); printf("fabs(%.1f) = %.1fn", z,fabs(z)); return 0; } Running the above program will produce output similar to the following: sqrt(2.0) = 1.41 pow(2.0, 3.0) = 8.00 exp(2.0) = 7.39 log(2.0) = 0.69 log10(2.0) = 0.30 sin(0.79) = 0.71 cos(0.79) = 0.71 tan(0.79) = 1.00 ceil(5.8) = 6.0 floor(5.8) = 5.0 fabs(5.8) = 5.8 `` provides a comprehensive set of mathematical functions and constants, assisting programmers in performing various mathematical calculations in C. By using these functions and constants, complex mathematical operations can be implemented easily, thereby improving program efficiency and readability.
← C Standard Library Setjmp HC Standard Library Locale H β†’