Skip to content

Core\Math::atanh

Returns the number whose hyperbolic tangent is the number you give it. This is the inverse hyperbolic tangent, and it undoes Core\Math::tanh.

The number must be from -1.0 to 1.0, because a hyperbolic tangent is always in that range. 1.0 gives INF (infinity), and -1.0 gives -INF. A number outside the range gives NaN (a value that means "not a number"), and Core\Math::isNan tests for it.

In plain words: tanh squeezes any number into the range from -1.0 to 1.0. atanh stretches a number from that range back out. Numbers close to 1.0 or -1.0 become very big.

The examples below show a few results, then what happens at the ends of the range and outside it, then how a report finds the average of several correlations.

Parameters

  • $nfloatA hyperbolic tangent: a number from -1.0 to 1.0.

Return value

Returns float. A number with the same sign as $n. 1.0 gives INF and -1.0 gives -INF. A $n outside -1.0 to 1.0 gives NaN.

Examples

Undoing tanh

<?nvs
// Shows a few results of `Core\Math::atanh`, and how it undoes `Core\Math::tanh`.
// This prints 0.54930614433405.
echo Core\Math::atanh(0.5), "\n";
// A negative number gives a negative result. This prints -0.54930614433405.
echo Core\Math::atanh(-0.5), "\n";
// `Core\Math::tanh` turns 2.0 into 0.96402758007582.
// `Core\Math::atanh` turns that back into 2.0. This prints 2.
echo Core\Math::atanh(Core\Math::tanh(2.0)), "\n";
Output
0.54930614433405
-0.54930614433405
2

The ends of the range

<?nvs
// Shows what `Core\Math::atanh` returns close to the ends of its range, at the ends, and outside it.
// A number close to 1.0 gives a big result. This prints 3.8002011672502.
echo Core\Math::atanh(0.999), "\n";
// 1.0 gives infinity. This prints INF.
echo Core\Math::atanh(1.0), "\n";
// -1.0 gives minus infinity. This prints -INF.
echo Core\Math::atanh(-1.0), "\n";
// 1.5 is outside the range, so the result is `NaN`.
// `Core\Math::isNan` tests for it. This prints "not a number".
float $result = Core\Math::atanh(1.5);
echo Core\Math::isNan($result) ? "not a number\n" : "a number\n";
Output
3.8002011672502
INF
-INF
not a number

The average of correlations

<?nvs
// Shows how a report finds the average of several correlations.
// A correlation is a number from -1.0 to 1.0 that says how closely two things move together.
class Correlation
{
// Correlations are not averaged directly. `atanh` converts each one to a number
// that can be averaged. `tanh` converts the average back to a correlation.
public static function average(array<float> $values): float
{
float $sum = 0.0;
int $count = 0;
foreach ($values as float $value) {
$sum = $sum + Core\Math::atanh($value);
$count = $count + 1;
}
return Core\Math::round(Core\Math::tanh($sum / $count), { precision: 2 });
}
}
// Three stores measured how closely rain and umbrella sales move together.
// This prints 0.82.
echo Correlation::average([0.7, 0.8, 0.9]), "\n";
// A plain average of the same three numbers is 0.8. This prints 0.8.
echo Core\Math::round((0.7 + 0.8 + 0.9) / 3.0, { precision: 2 }), "\n";
Output
0.82
0.8