Compute the hyperbolic tangent, a smooth saturating function bounded by -1 and 1.
TANH(expr)
## Overview Returns the hyperbolic tangent of the input: TANH(x) = SINH(x) / COSH(x) = (e^x - e^-x) / (e^x + e^-x). TANH is a smooth, anti-symmetric saturating function bounded by -1 and 1. It is widely used in machine learning as an activation function, in signal processing as a soft clipping nonlinearity, and in physics to map rapidity to velocity as a fraction of c. Unlike SINH and COSH, TANH does not overflow: its bounded range makes it numerically stable for arbitrary inputs. It asymptotes to +/-1 smoothly and rapidly, reaching within one ULP of the limit by magnitude 19 or so. ## Behavior - Domain: all finite DOUBLE values. - Range: (-1, 1). In floating-point the range is actually [-1, 1] because for large magnitudes the result rounds to exactly +/-1. - Returns NULL if the argument is NULL. - Is anti-symmetric: TANH(-x) = -TANH(x). - Saturates to +/-1 for large magnitudes. ## Numeric precision - Uses the IEEE 754 double-precision tanh routine. Accurate to within one ULP. - Does not overflow and is numerically stable over the entire real line. - For inputs with magnitude above approximately 19, TANH rounds to exactly +/-1.0. ## Compatibility - Conforms to the SQL standard definition of TANH as a scalar DOUBLE function. - Matches typical mathematical library implementations.
| Name | Type | Description |
|---|---|---|
expr | Specifies the real-valued input. Accepts any finite DOUBLE value. |
-- Basic literal
SELECT TANH(0);
-- Result: 0.0
-- Hyperbolic tangent of 1
SELECT TANH(1);
-- Result: 0.7615941559557649
-- Large positive input saturates at 1
SELECT TANH(100);
-- Result: 1.0
-- Anti-symmetry: TANH(-x) = -TANH(x)
SELECT TANH(-2), -TANH(2);
-- Result: -0.9640275800758169, -0.9640275800758169
-- Definition via SINH and COSH
SELECT TANH(0.5), SINH(0.5) / COSH(0.5);
-- Result: 0.46211715726000974, 0.46211715726000974
-- Column use: smoothly compress a score into [-1, 1] for feature engineering
SELECT customer_id, TANH(raw_score / 100.0) AS bounded_score
FROM analytics.ml.customer_scores;