SQL TANH function
Returns the hyperbolic tangent: TANH(0) is 0, values approach 1 for large positive x and -1 for large negative x.
- Categoria
- Math
- Returns
float / double- In this category
- 43 of 82
Sintaxe
TANH(number)
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No hyperbolic functions; compute (EXP(x) - EXP(-x)) / (EXP(x) + EXP(-x)) |
| PostgreSQL | Sim | Built in since PostgreSQL 12 |
| MySQL | Not built in | No hyperbolic functions in MySQL 8.4; use the EXP identity |
| SQLite | Sim | Built in on 3.35+ |
| Oracle | Sim | Built in |
Exemplos
PostgreSQL
SELECT TANH(0) AS zero, TANH(100) AS capped; -- 0, 1
SQL Server
SELECT (EXP(1.0) - EXP(-1.0)) / (EXP(1.0) + EXP(-1.0)) AS tanh_one; -- 0.7615
PostgreSQL
SELECT TANH(score / 10.0) AS normalized FROM rankings;
Bom saber
- Mathematically the result is in (-1, 1), but for |x| beyond about 19.1 the double rounds to exactly 1.0 or -1.0.
- TANH never overflows for large inputs even though SINH and COSH do (the ratio converges), but the SQL Server EXP-based fallback can overflow in both terms.
- It is SINH(x) / COSH(x); the identity is also the safest portable definition.
Questions about TANH
What range does TANH return?
Strictly between -1 and 1 in theory. Large inputs saturate: from about |x| = 19.1 the double result is exactly 1.0 or -1.0.
How do I compute TANH on SQL Server or MySQL?
(EXP(x) - EXP(-x)) / (EXP(x) + EXP(-x)). Neither has native hyperbolic functions. Guard against EXP overflow for |x| above about 709, where TANH is 1 or -1 anyway.
Why use TANH for normalization?
It squashes any real number smoothly into (-1, 1) with a stable midpoint at 0, handy for scoring columns without a known maximum.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.