math.h in C Language
Type and Macros in math.h
The math.h
header defines two type aliases:
- float_t: The type that provides the most efficient execution of
float
operations on the current system, with a width that is at least equal to that of afloat
. - double_t: The type that provides the most efficient execution of
double
operations on the current system, with a width that is at least equal to that of adouble
.
You can determine their specific types using the FLT_EVAL_METHOD
macro.
Mapping of FLT_EVAL_METHOD Values
FLT_EVAL_METHOD Value | Type for float_t | Type for double_t |
---|---|---|
0 | float | double |
1 | double | double |
2 | long double | long double |
Other | Implementation Defined | Implementation Defined |
Additionally, math.h
defines several macros:
- INFINITY: Represents positive infinity and returns a
float
type value. - NAN: Represents Not-A-Number (NaN) and returns a
float
type value.
Error Types in Mathematical Functions
Mathematical functions can produce the following types of errors:
- Range Errors: The result cannot be represented by the function’s return type.
- Domain Errors: Function parameters are invalid for the given function.
- Pole Errors: Parameters cause the function’s limit to become infinite.
- Overflow Errors: The result is too large, leading to overflow.
- Underflow Errors: The result is too small, leading to underflow.
The variable math_errhandling
indicates how the current system handles mathematical errors.
Values of math_errhandling
Value | Description | |
---|---|---|
MATH_ERRNO | The system uses errno to indicate mathematical errors. |
|
MATH_ERREXCEPT | The system uses exceptions to indicate mathematical errors. | |
MATH_ERRNO | MATH_ERREXCEPT | The system uses both methods to indicate mathematical errors. |
Value Type
The parameters of a mathematical function can be categorized into the following types: normal values, infinite values, finite values, and non-numeric values.
The following functions are used to determine the type of a given value:
- fpclassify(): Classifies the given floating-point number.
- isfinite(): Returns true if the parameter is neither infinite nor NaN.
- isinf(): Returns true if the parameter is infinite.
- isnan(): Returns true if the parameter is not a number.
- isnormal(): Returns true if the parameter is a normal number.
Here’s an example:
1 | isfinite(1.23) // 1 |
signbit()
Checks whether the parameter has a sign. It returns 1 if the parameter is negative and 0 otherwise.
1 | signbit(3490.0) // 0 |
Trigonometric Functions
The following are trigonometric functions that take radians as parameters:
- acos(): Arccosine
- asin(): Arcsine
- atan(): Arctangent
- atan2(): Arctangent of two variables
- cos(): Cosine
- sin(): Sine
- tan(): Tangent
Remember, all of these functions have a float version (with an “f” suffix) and a long double version (with an “l” suffix).
Here’s an example:
1 | cos(PI/4) // 0.707107 |
Here’s a more natural English expression for your content:
Hyperbolic Functions
The following are hyperbolic functions that take floating-point parameters:
- acosh(): Inverse hyperbolic cosine.
- asinh(): Inverse hyperbolic sine.
- atanh(): Inverse hyperbolic tangent.
- cosh(): Hyperbolic cosine.
- sinh(): Hyperbolic sine.
- tanh(): Hyperbolic tangent.
Exponential and Logarithmic Functions
The following are exponential and logarithmic functions, all returning a value of type double
:
- exp(): Computes Euler’s number ( e ) raised to the power of ( x ) (i.e., ( $e^x$ )).
- exp2(): Computes ( 2 ) raised to the power of ( x ) (i.e., ( $ 2^x $ )).
- expm1(): Computes ( $ e^x - 1 $ ).
- log(): Computes the natural logarithm, the inverse of
exp()
. - log2(): Computes the logarithm base ( 2 ).
- log10(): Computes the logarithm base ( 10 ).
- log1p(): Computes the natural logarithm of ( x + 1 ) (i.e., ( $ \ln(x + 1) $ )).
- logb(): Computes the logarithm with base defined by the macro
FLT_RADIX
(typically ( 2 )), returning only the integer part.
Examples
1 | exp(3.0); // 20.085500 |
If the result exceeds the maximum value representable in C, the function returns HUGE_VAL
, which is defined in math.h
as a double
type value.
If the result is too small to be represented as a double
, the function returns 0
. Both cases are considered errors.
frexp()
The frexp()
function decomposes a floating-point number into its fractional and exponent parts, using 2 as the base. For example, the number 1234.56 can be expressed as ( $ 0.6028125 \times 2^{11} $ ). This function returns the fractional part and provides the exponent.
Function Signature:
1 | double frexp(double value, int* exp); |
- Parameters:
value
: The floating-point number to decompose.exp
: A pointer to an integer where the exponent will be stored.
The function returns the fractional part and assigns the exponent to the variable pointed to by exp
. If the input is 0, both the fractional part and the exponent will be 0.
Example:
1 | double frac; |
ilogb()
The ilogb()
function returns the exponent of a floating-point number, with the base defined by the macro FLT_RADIX
(usually 2).
Function Signature:
1 | int ilogb(double x); |
- Parameter:
x
: The floating-point number whose exponent is to be determined.
The return value is ( $\text{log}_r |x|$ ), where ( r ) is the macro FLT_RADIX
.
Usage Examples:
1 | ilogb(257); // Returns 8 |
ldexp()
The ldexp()
function multiplies a number by 2 raised to a specified power. It can be seen as the inverse of frexp()
, combining a fractional part and an exponent into the form ( $f\times2^n$).
Function Signature:
1 | double ldexp(double x, int exp); |
- Parameters:
x
: The multiplicand.exp
: The exponent of 2.
The function returns ( $ x \times 2^{\text{exp}} $ ).
Usage Examples:
1 | ldexp(1, 10); // Returns 1024.000000 |
modf()
The modf()
function separates a number into its integer and fractional parts.
Function Signature:
1 | double modf(double value, double* iptr); |
- Parameters:
value
: The number to be separated.iptr
: A pointer to a double variable where the integer part will be stored.
The function returns the fractional part, while the integer part is stored in the variable pointed to by iptr
.
Example:
1 | double int_part; |
Here’s an optimized version of your text, tailored to sound more natural in English:
scalbn()
The scalbn()
function computes the value of ( $ x \times r^n $ ), where ( r ) is defined by the macro FLT_RADIX
.
1 | double scalbn(double x, int n); |
It takes two parameters: the first parameter ( x ) is the base, and the second parameter ( n ) is the exponent. It returns the result of ( $ x \times r^n $ ).
Examples:
1 | scalbn(2, 8); // returns 512.000000 |
There are several versions of this function:
scalbn()
: uses anint
for the exponent ( n ).scalbnf()
: thefloat
version ofscalbn()
.scalbnl()
: thelong double
version ofscalbn()
.scalbln()
: uses along int
for the exponent ( n ).scalblnf()
: thefloat
version ofscalbln()
.scalblnl()
: thelong double
version ofscalbln()
.
round()
The round()
function rounds a number to the nearest integer, using standard rounding rules (e.g., 1.5 rounds to 2, -1.5 rounds to -2).
1 | double round(double x); |
It returns a floating-point number.
Examples:
1 | round(3.14); // returns 3.000000 |
Other versions include:
lround()
: returns along int
.llround()
: returns along long int
.
trunc()
The trunc()
function truncates the decimal part of a floating-point number and returns the integer part as a floating-point value.
1 | double trunc(double x); |
Examples:
1 | trunc(3.14); // returns 3.000000 |
ceil()
The ceil()
function returns the smallest integer value that is greater than or equal to its argument (rounded up).
1 | double ceil(double x); |
Examples:
1 | ceil(7.1); // returns 8.0 |
floor()
The floor()
function returns the largest integer value that is less than or equal to its argument (rounded down).
1 | double floor(double x); |
Examples:
1 | floor(7.1); // returns 7.0 |
Custom Rounding Function
The following function implements rounding to the nearest integer:
1 | double round_nearest(double x) { |
fmod()
The fmod()
function computes the remainder of the division of the first parameter by the second. It serves as the floating-point equivalent of the modulus operator %
, which is limited to integers.
1 | double fmod(double x, double y); |
It effectively calculates ( $ x - \text{trunc}(x / y) \times y $ ), ensuring that the sign of the result matches that of ( x ).
Examples:
1 | fmod(5.5, 2.2); // returns 1.100000 |
Sure! Here’s an optimized version of your text for clarity and fluency in English:
Floating-Point Comparison Functions
The following functions are used to compare two floating-point numbers and return an integer result:
- isgreater(x, y): Returns 1 if ( x > y ), otherwise returns 0.
- isgreaterequal(x, y): Returns 1 if ( $ x \geq y $ ), otherwise returns 0.
- isless(x, y): Returns 1 if ( x < y ), otherwise returns 0.
- islessequal(x, y): Returns 1 if ( $ x \leq y $ ), otherwise returns 0.
- islessgreater(x, y): Returns 1 if ( x < y ) or ( x > y ) (i.e., not equal), otherwise returns 0.
Examples:
1 | isgreater(10.0, 3.0) // 1 |
isunordered()
- isunordered(x, y): Returns 1 if either argument is NaN (Not a Number), otherwise returns 0.
Example:
1 | isunordered(1.0, 2.0) // 0 |
Other Functions in <math.h>
The <math.h>
library includes several other useful functions:
- pow(x, y): Computes ( x ) raised to the power of ( y ).
- sqrt(x): Computes the square root of ( x ).
- cbrt(x): Computes the cube root of ( x ).
- fabs(x): Computes the absolute value of ( x ).
- hypot(x, y): Computes the length of the hypotenuse given the lengths of the two perpendicular sides ( x ) and ( y ).
- fmax(x, y): Returns the larger of ( x ) and ( y ).
- fmin(x, y): Returns the smaller of ( x ) and ( y ).
- remainder(x, y): Computes the remainder according to the IEC 60559 standard.
- remquo(x, y): Computes the remainder and quotient at the same time.
- copysign(x, y): Returns a value with the magnitude of ( x ) and the sign of ( y ).
- nan(): Returns a NaN value.
- nextafter(x, y): Computes the next representable floating-point value after ( x ) in the direction of ( y ).
- fdim(x, y): Returns ( x - y ) if positive, otherwise returns 0.
- fma(x, y, z): Computes ( $ x \times y + z $ ) in a single operation.
- nearbyint(x): Rounds ( x ) to the nearest integer using the current rounding mode.
- rint(x): Rounds ( x ) to the nearest integer and may raise the INEXACT exception.
- lrint(x): Rounds ( x ) to the nearest integer and returns it as a long integer.
- erf(x): Computes the error function of ( x ).
- erfc(x): Computes the complementary error function of ( x ).
- tgamma(x): Computes the Gamma function of ( x ).
- lgamma(x): Computes the natural logarithm of the absolute value of the Gamma function.
Examples:
1 | pow(3, 4) // 81.000000 |