Core\Math::tanh
Returns the hyperbolic tangent of a number. That is Core\Math::sinh divided by Core\Math::cosh.
The result is always from -1.0 to 1.0, and it has the same sign as the number. It is 0.0 at zero. For large numbers it gets very close to 1.0, and from about 20 it is exactly 1.0. INFINITY gives 1.0 and -INFINITY gives -1.0. NaN (a value that means "not a number") gives NaN.
In plain words: Core\Math::tanh turns any number into a number from -1.0 to 1.0. A number close to zero changes very little. A large number ends up close to 1.0 or -1.0.
The examples below show a few results, then large numbers, then how a website turns votes of any size into a score from -1 to 1.
Parameters
$nfloatAny number.
Return value
Returns float. A number from -1.0 to 1.0, with the same sign as $n. INFINITY gives 1.0, -INFINITY gives -1.0 and NaN gives NaN.
Examples
A few results
<?nvs// Shows the hyperbolic tangent of a few numbers.
// The result at zero is 0. This prints 0.echo Core\Math::tanh(0.0), "\n";
// A negative number gives the same result with a minus sign.// These lines print 0.46211715726001 and -0.46211715726001.echo Core\Math::tanh(0.5), "\n";echo Core\Math::tanh(-0.5), "\n";00.46211715726001-0.46211715726001Large numbers
<?nvs// Shows that the hyperbolic tangent never goes past 1 or -1.
// At 3 the result is already close to 1. This prints 0.99505475368673.echo Core\Math::tanh(3.0), "\n";
// At 20 the result is exactly 1. This prints yes.echo Core\Math::tanh(20.0) == 1.0 ? 'yes' : 'no', "\n";
// An infinity gives 1 or -1. These lines print 1 and -1.echo Core\Math::tanh(Core\Math::INFINITY), "\n";echo Core\Math::tanh(-Core\Math::INFINITY), "\n";0.99505475368673yes1-1A score from votes
<?nvs// Shows how a website turns the votes on a post into a score from -1 to 1.
class Post{ // Each group of 10 more likes than dislikes moves the score closer to 1. public static function score(float $likes, float $dislikes): float { float $value = Core\Math::tanh(($likes - $dislikes) / 10.0); return Core\Math::round($value, { precision: 2 }); }}
// 12 likes and 2 dislikes. This prints 0.76.echo Post::score(12.0, 2.0), "\n";
// More dislikes than likes gives a negative score. This prints -0.2.echo Post::score(3.0, 5.0), "\n";
// A very popular post gets 1, and no post can get more. This prints 1.echo Post::score(500.0, 20.0), "\n";0.76-0.21