Why Use C Functions?
- Trigonometric Functions
|
Function |
What It Does |
Example Code |
Output |
|---|---|---|---|
|
acos(x) |
Arccosine of x (radians) |
printf("%f", acos(1)); |
0.000000 |
|
acosh(x) |
Hyperbolic arccosine |
printf("%f", acosh(1)); |
0.000000 |
|
asin(x) |
Arcsine of x (radians) |
printf("%f", asin(1)); |
1.570796 |
|
asinh(x) |
Hyperbolic arcsine |
printf("%f", asinh(1)); |
0.881374 |
|
atan(x) |
Arctangent (radians) |
printf("%f", atan(1)); |
0.785398 |
|
atan2(y,x) |
Angle for coordinate (x,y) |
printf("%f", atan2(1,1)); |
0.785398 |
|
atanh(x) |
Hyperbolic arctangent |
printf("%f", atanh(0.5)); |
0.549306 |
|
cosh(x) |
Hyperbolic cosine |
printf("%f", cosh(0)); |
1.000000 |
|
sin(x) |
Sine of x |
printf("%f", sin(0)); |
0.000000 |
|
sinh(x) |
Hyperbolic sine |
printf("%f", sinh(0)); |
0.000000 |
|
tan(x) |
Tangent |
printf("%f", tan(0)); |
0.000000 |
|
tanh(x) |
Hyperbolic tangent |
printf("%f", tanh(0)); |
0.000000 |
- Power & Exponential Functions
|
Function |
What It Does |
Example Code |
Output |
|---|---|---|---|
|
exp(x) |
e^x |
printf("%f", exp(1)); |
2.718282 |
|
exp2(x) |
2^x |
printf("%f", exp2(3)); |
8.000000 |
|
expm1(x) |
e^x − 1 |
printf("%f", expm1(1)); |
1.718282 |
|
pow(x,y) |
x^y |
printf("%f", pow(2,3)); |
8.000000 |
|
sqrt(x) |
Square root |
printf("%f", sqrt(16)); |
4.000000 |
|
cbrt(x) |
Cube root |
printf("%f", cbrt(27)); |
3.000000 |
|
hypot(x,y) |
√(x² + y²) |
printf("%f", hypot(3,4)); |
5.000000 |
- Rounding Functions
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
ceil(x) |
Round up |
printf("%f", ceil(4.3)); |
5.000000 |
|
floor(x) |
Round down |
printf("%f", floor(4.8)); |
4.000000 |
|
round(x) |
Nearest integer |
printf("%f", round(4.6)); |
5.000000 |
|
trunc(x) |
Remove decimals |
printf("%f", trunc(4.9)); |
4.000000 |
|
rint(x) |
Round using current mode |
printf("%f", rint(2.5)); |
(system-dependent) |
|
nearbyint(x) |
Round (no exceptions) |
printf("%f", nearbyint(2.3)); |
2.000000 |
- Logarithmic Functions
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
log(x) |
Natural log |
printf("%f", log(10)); |
2.302585 |
|
log10(x) |
Base 10 log |
printf("%f", log10(100)); |
2.000000 |
|
log1p(x) |
log(1+x) |
printf("%f", log1p(1)); |
0.693147 |
|
log2(x) |
Base 2 log |
printf("%f", log2(8)); |
3.000000 |
|
logb(x) |
Exponent of x |
printf("%f", logb(16)); |
4.000000 |
|
ilogb(x) |
Integer exponent |
printf("%d", ilogb(16)); |
4 |
- Floating-Point Utilities
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
fabs(x) |
Absolute value |
printf("%f", fabs(-5)); |
5.000000 |
|
fdim(x,y) |
Positive difference |
printf("%f", fdim(8,3)); |
5.000000 |
|
fmax(x,y) |
Max value |
printf("%f", fmax(5,10)); |
10.000000 |
|
fmin(x,y) |
Min value |
printf("%f", fmin(5,10)); |
5.000000 |
|
fmod(x,y) |
Floating remainder |
printf("%f", fmod(10,3)); |
1.000000 |
|
remainder(x,y) |
IEEE remainder |
printf("%f", remainder(10,3)); |
1.000000 |
|
copysign(x,y) |
Copy sign |
printf("%f", copysign(5,-1)); |
-5.000000 |
|
nan("") |
Not a Number |
printf("%f", nan("")); |
nan |
- Splitting & Manipulating Numbers
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
frexp(x, &n) |
Returns mantissa |
int n; printf("%f", frexp(8,&n)); |
0.500000 |
|
ldexp(x,n) |
x × 2ⁿ |
printf("%f", ldexp(1,3)); |
8.000000 |
|
modf(x, &n) |
Fractional part |
double n; printf("%f", modf(5.75,&n)); |
0.750000 |
- Next Representable Values
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
nextafter(x,y) |
Next float toward y |
printf("%g", nextafter(1,2)) |
Very small + change |
|
nexttoward(x,y) |
Higher precision version |
Same as above |
Very small + change |
- Integer Rounding (Long & Long Long)
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
lrint(x) |
Round to long |
printf("%ld", lrint(5.6)); |
6 |
|
lround(x) |
Nearest integer (long) |
printf("%ld", lround(5.4)); |
5 |
|
llrint(x) |
Round to long long |
printf("%lld", llrint(5.6)); |
6 |
|
llround(x) |
Nearest integer (long long) |
printf("%lld", llround(5.4)); |
5 |
- Advanced Math
|
Function |
Description |
Example Code |
Output |
|---|---|---|---|
|
tgamma(x) |
Gamma function |
printf("%f", tgamma(5)); |
24.000000 |
|
lgamma(x) |
Log gamma |
printf("%f", lgamma(5)); |
3.178054 |
|
erf(x) |
Error function |
printf("%f", erf(1)); |
0.842701 |
|
erfc(x) |
Complementary error function |
printf("%f", erfc(1)); |
0.157299 |
|
scalbn(x,n) |
x × 2ⁿ |
printf("%f", scalbn(3,2)); |
12.000000 |
|
scalbln(x,n) |
Long integer version |
printf("%f", scalbln(3,2)); |
12.000000 |