Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library

C Standard Library

is a header file in the C Standard Library that providestype-generic mathematical functions (Type-Generic Math Functions).

The library was introduced in the C99 standard, allowing developers to use unified function names to call mathematical functions of different types (such as float, double and long double), without having to explicitly specify the concrete type of the function.

The main purposes are:


1. Type-generic macros

Define a set of type-generic macros that automatically select the correct math function based on the argument type. For example:

These macros support the following argument types:


2. Supported functions

The following type-generic math functions are supported:

Basic math functions

function Description
sin Sine function
cos Cosine function
tan Tangent function
asin Arcsine function
acos Arccosine function
atan Arctangent function
atan2 Arctangent function with two arguments
sinh Hyperbolic sine function
cosh Hyperbolic cosine function
tanh Hyperbolic tangent function
asinh Inverse hyperbolic sine function
acosh Inverse hyperbolic cosine function
atanh Inverse hyperbolic tangent function

Exponential and logarithmic functions

function Description
exp Exponential function
log Natural logarithm function
log10 Common logarithm function
pow Power function

Other functions

function Description
sqrt Square root function
fabs Absolute value function
ceil Ceiling function
floor Floor function
fmod Floating-point remainder function

3. Example

The following is an example of using Example showing how to use type-generic math functions:

Example

#include

#include

int

main

(

)

{

   

// Real number types

   

float

f

=

1.5f

;

   

double

d

=

2.5

;

   

long

double

ld

=

3.5L

;

   

// Using type-generic functions

   

printf

(

"sin(f) = %f\n"

,

sin

(

f

)

)

;

     

// Calls sinf

   

printf

(

"sin(d) = %f\n"

,

sin

(

d

)

)

;

     

// Calls sin

   

printf

(

"sin(ld) = %Lf\n"

,

sin

(

ld

)

)

;

   

// Calls sinl

   

// Complex number types

   

double

complex z

=

1.0

2.0

I

;

   

// Using type-generic functions

   

double

complex sqrt_z

=

sqrt

(

z

)

;

   

printf

(

"sqrt(z) = %f + %fi\n"

,

creal

(

sqrt_z

)

,

cimag

(

sqrt_z

)

)

;

   

return

0

;

}

Output result:

sin(f) = 0.997495

sin(d) = 0.598472

sin(ld) = 0.350783

sqrt(z) = 1.272020 + 0.786151i

4. Notes


5. Implementation principle

The implementation relies on C's generic selection mechanism (_Generic keyword). For example,sin The macro definition might look like this:

#define sin(x) _Generic((x), \

    float: sinf,             \

    double: sin,             \

    long double: sinl        \

)(x)

_Generic The keyword selects the correct function based on the x type of the argument.

provides type-generic math functions, simplifying calls to math functions for different numeric types (such as float, double, long double and complex types). It is an important feature of the C99 standard, especially suited to scientific computing and engineering applications that need to handle multiple numeric types. By using , developers can write more concise and more general code.