A Quick Look TANH() in SQLite

The tanh() function in SQLite calculates the hyperbolic tangent of a number.

The hyperbolic tangent is the ratio of hyperbolic sine to hyperbolic cosine, producing values between -1 and 1.

Syntax

tanh(x)

where x is a numeric expression in radians.

Example 1

Here’s an example to demonstrate:

SELECT tanh(2.3);

Output:

+-------------------+
| tanh(2.3) |
+-------------------+
| 0.980096396266191 |
+-------------------+

Example 2

This example uses tanh() against data from a database:

-- Create a sample table
CREATE TABLE angles (x FLOAT);

-- Insert some values
INSERT INTO angles VALUES (0), (1), (-1), (2);

-- Calculate tanh for each value
SELECT x, tanh(x) FROM angles;

Output:

+------+--------------------+
| x | tanh(x) |
+------+--------------------+
| 0.0 | 0.0 |
| 1.0 | 0.761594155955765 |
| -1.0 | -0.761594155955765 |
| 2.0 | 0.964027580075817 |
+------+--------------------+

Note that:

  • tanh(0) = 0
  • As x approaches large positive numbers, tanh(x) approaches 1
  • As x approaches large negative numbers, tanh(x) approaches -1
  • The function accepts both integers and floating-point numbers
  • The result is always returned as a floating-point number

Passing Null Values

Passing null results in null:

SELECT tanh(null);

Output:

+------------+
| tanh(null) |
+------------+
| null |
+------------+

Passing the Wrong Data Type

Passing the wrong data type results in null:

SELECT tanh('two');

Output:

+-------------+
| tanh('two') |
+-------------+
| null |
+-------------+