tanh
Use the tanh() function to calculate the hyperbolic tangent of a numeric value.
Syntax
tanh(<number>)Parameters
number
integer, float
Yes
The numeric value for which to calculate the hyperbolic tangent. Any real number is valid.
Returns
Type: float
Description: The tanh() function returns the hyperbolic tangent of the input value. The resulting value is within the range (-1, 1). If the input is null, the function returns null.
Usage notes
Input type: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.
Valid Input Range: The
tanh()function accepts any real number as input.Null Handling: If the input expression is
null, the function returnsnull.Symmetry: The function is odd, meaning
tanh(-x) = -tanh(x).Identity Value:
tanh(0)always returns0.0.Result Range: The output is always strictly between -1 and 1 (exclusive). As
xapproaches positive infinity,tanh(x)approaches1.0. Asxapproaches negative infinity,tanh(x)approaches-1.0.Definition: The hyperbolic tangent is defined as
sinh(x) / cosh(x)or(e^x - e^(-x)) / (e^x + e^(-x)).Common Use Cases: This function is typically used within the
alterstage for data normalization (squashing values to the range (-1, 1)), neural network activation functions, and scientific computations.
Examples
Example 1: Calculate hyperbolic tangent of literal values
Goal: Calculate the hyperbolic tangent for specific numeric literals to verify known mathematical results.
XQL code:
Explanation: tanh(0) returns 0.0. tanh(1) returns approximately 0.7616, and tanh(-1) returns approximately -0.7616, demonstrating the odd symmetry of the function.
Output:
0.0
0.76159
-0.76159
Example 2: Calculate hyperbolic tangent from a field value
Goal: Calculate the hyperbolic tangent of values stored in a dataset field.
XQL code:
Explanation: This query computes the hyperbolic tangent for each value in the numeric_value field. The result is always between -1 and 1, effectively normalizing the values. No filtering is needed since tanh() accepts any real number.
Output:
101
0.0
0.0
102
1.0
0.76159
103
100.0
1.0
Example 3: Use tanh for data normalization
Goal: Normalize large numeric values to the range (-1, 1) using the hyperbolic tangent function.
XQL code:
Explanation: This query first scales numeric_value by dividing by 100, then applies tanh() to squash the result into the range (-1, 1). This is a common normalization technique that preserves the sign and relative magnitude of values while bounding the output.
Output:
101
5.0
0.04996
102
500.0
1.0
103
-200.0
-0.96403
Related articles
Datasets:
sample_xql_raw,xdr_dataXQL recipes: XQL Recipes
Was this helpful?
