Example 1 — Common atanh Values
Print results for zero and a pair of opposite inputs.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("atanh(0) = %.4f\n", atanh(0.0));
printf("atanh(0.5) = %.4f\n", atanh(0.5));
printf("atanh(-0.5) = %.4f\n", atanh(-0.5));
return 0;
}
atanh(0) = 0.0000
atanh(0.5) = 0.5493
atanh(-0.5) = -0.5493
How It Works
- Include math.h. Declares
atanh; link with -lm on GCC. - Zero maps to zero.
atanh(0) is 0. - Positive half.
atanh(0.5) ≈ 0.5493. - Odd function.
atanh(-0.5) is the negative of atanh(0.5).
Example 2 — Check with tanh
Round-trip a value: tanh(atanh(x)) should recover x on the valid domain.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 0.75;
double t = atanh(x);
printf("atanh(%.2f) = %.5f\n", x, t);
printf("tanh(t) = %.5f\n", tanh(t));
return 0;
}
atanh(0.75) = 0.97296
tanh(t) = 0.75000
How It Works
- Start in domain.
0.75 satisfies |x| < 1. - Invert with atanh. You get a real
t. - Apply tanh.
tanh(t) returns to 0.75. - Inverse pair. Use this to verify your understanding of
atanh and tanh.
Example 3 — Guard the Domain
Reject |x| ≥ 1 before calling atanh so you do not print a quiet NaN by surprise.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 1.0;
if (x <= -1.0 || x >= 1.0) {
printf("x out of domain for atanh\n");
} else {
printf("atanh(%.1f) = %.4f\n", x, atanh(x));
}
return 0;
}
x out of domain for atanh
How It Works
- Endpoints are invalid.
atanh(1) and atanh(-1) are not defined as real numbers. - Check before the call. Compare against
-1 and 1. - Avoid silent NaN. A clear message is easier to debug than
nan in output. - Library may set errno. See
errno / EDOM for domain errors if you call anyway.