View a markdown version of this page

Trigonometric and hyperbolic functions - Amazon Athena

Trigonometric and hyperbolic functions

Use these functions to compute the sine, cosine, tangent, and cotangent of an angle, the hyperbolic sine, cosine, and tangent of a number, and their inverses. You can also convert angles between degrees and radians. The trigonometric functions take and return angles in radians.

acos function

Returns the angle, in radians, whose cosine is x. The result is from 0 to pi, which is about 3.14159. If x is NULL, the result is NULL.

To convert the result to degrees, use the degrees function. To compute the cosine of an angle, use the cos function.

Syntax

acos(x)

Arguments

The acos function takes the following argument:

x

The cosine of the angle that the function returns, as a DOUBLE value from -1 to 1. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the arccosine of 0.5, in radians.

SELECT acos(0.5) AS result

The query returns the following result.

result

1.0471975511965979

The following SQL query converts the arccosine of 0.5 to degrees and rounds it to six decimal places. It also returns the arccosine of -1, which is pi.

SELECT round(degrees(acos(0.5)), 6) AS in_degrees, acos(-1) AS pi_radians

The query returns the following result.

in_degrees pi_radians

60.0

3.141592653589793

The following SQL query uses isnan to show that a value greater than 1 returns NaN, and shows that a NULL input returns NULL.

SELECT isnan(acos(2)) AS out_of_range, acos(NULL) AS null_input

The query returns the following result.

out_of_range null_input

true

NULL

Considerations

If x is less than -1 or greater than 1, the result is NaN, and the query does not fail. To test for this result, use the isnan function.

The result is approximate. For example, degrees(acos(0.5)) can return a value slightly different from 60, so round the result before you compare or display it, as in round(degrees(acos(0.5)), 6).

acosh function

Returns the value whose hyperbolic cosine is x. The result is 0 or greater, and acosh(1) returns 0.0. If x is NULL, the result is NULL.

To compute the hyperbolic cosine of a value, use the cosh function.

Syntax

acosh(x)

Arguments

The acosh function takes the following argument:

x

The hyperbolic cosine of the value that the function returns, as a DOUBLE value of 1 or greater. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the inverse hyperbolic cosine of 2.3.

SELECT acosh(2.3) AS result

The query returns the following result.

result

1.4750447812414251

The following SQL query shows that acosh(1) returns 0.0, and uses isnan to show that a value less than 1 returns NaN.

SELECT acosh(1) AS at_one, isnan(acosh(0.5)) AS below_one

The query returns the following result.

at_one below_one

0.0

true

The following SQL query shows that cosh reverses acosh. The result is rounded, because it is approximate.

SELECT round(cosh(acosh(2.3)), 10) AS round_trip

The query returns the following result.

round_trip

2.3

Considerations

If x is less than 1, the result is NaN, and the query does not fail. To test for this result, use the isnan function.

asin function

Returns the angle, in radians, whose sine is x. The result is between -pi() / 2 and pi() / 2. If x is NULL, the result is NULL.

To get the angle in degrees, pass the result to degrees. To compute the sine of an angle, use the sin function.

Syntax

asin(x)

Arguments

The asin function takes the following argument:

x

The sine of the angle that the function returns, as a DOUBLE value from -1 to 1. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the arcsine of 0.5, in radians.

SELECT asin(0.5) AS result

The query returns the following result.

result

0.5235987755982989

The following SQL query converts the arcsine of 0.5 to degrees and rounds it to six decimal places.

SELECT round(degrees(asin(0.5)), 6) AS result

The query returns the following result.

result

30.0

The following SQL query passes 2, which is outside the range from -1 to 1, and uses isnan to show that the result is NaN.

SELECT isnan(asin(2)) AS is_nan

The query returns the following result.

is_nan

true

Considerations

If x is less than -1 or greater than 1, the result is NaN (not a number), and the query does not fail. To find such results, test them with isnan.

The result is a floating-point approximation. Round it before you compare it with a constant, for example round(degrees(asin(x)), 6) = 30.

asinh function

Returns the inverse hyperbolic sine of x, which is the value whose hyperbolic sine is x. You can pass any real number as x, and the result has the same sign as x. If x is NULL, the result is NULL.

To compute the hyperbolic sine, use the sinh function.

Syntax

asinh(x)

Arguments

The asinh function takes the following argument:

x

The hyperbolic sine of the value that the function returns, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the inverse hyperbolic sine of 0.5.

SELECT asinh(0.5) AS result

The query returns the following result.

result

0.48121182505960347

The following SQL query shows that sinh reverses asinh. The result is rounded to ten decimal places.

SELECT round(sinh(asinh(2.5)), 10) AS result

The query returns the following result.

result

2.5

atan function

Returns the angle, in radians, whose tangent is x. The result is between -pi() / 2 and pi() / 2. If x is NULL, the result is NULL.

Because atan takes only the ratio of two coordinates, it cannot tell the point (1, 1) from the point (-1, -1). To get the angle of a point in any of the four quadrants, use the atan2 function.

Syntax

atan(x)

Arguments

The atan function takes the following argument:

x

The tangent of the angle that the function returns, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the arctangent of 0.5, in radians.

SELECT atan(0.5) AS result

The query returns the following result.

result

0.4636476090008061

The following SQL query converts the arctangent of 1 to degrees and rounds it to six decimal places.

SELECT round(degrees(atan(1)), 6) AS result

The query returns the following result.

result

45.0

Considerations

The result is a floating-point approximation. Round it before you compare it with a constant.

atan2 function

Returns the angle, in radians, between the positive x-axis and the point (x, y). The result is between -pi() and pi(). Unlike atan(y / x), atan2 uses the signs of both coordinates, so the result tells you which quadrant the point is in. If either argument is NULL, the result is NULL.

The y-coordinate is the first argument and the x-coordinate is the second.

Syntax

atan2(y, x)

Arguments

The atan2 function takes the following arguments:

y

The y-coordinate of the point, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

x

The x-coordinate of the point, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the angle of the point (0, 1), which is pi() / 2 radians. The y-coordinate, 1.0, is the first argument.

SELECT atan2(1.0, 0.0) AS result

The query returns the following result.

result

1.5707963267948966

The following SQL query compares atan and atan2 for the point (-1, -1), in degrees. atan sees only the ratio 1 and returns 45 degrees, while atan2 returns -135 degrees.

SELECT round( degrees(atan(-1.0 / -1.0)), 6 ) AS atan_degrees, round(degrees(atan2(-1, -1)), 6) AS atan2_degrees

The query returns the following result.

atan_degrees atan2_degrees

45.0

-135.0

Considerations

atan2(0, 0) returns 0, and the query does not fail.

atanh function

Returns the inverse hyperbolic tangent of x, which is the value whose hyperbolic tangent is x. If x is NULL, the result is NULL.

To compute the hyperbolic tangent, use the tanh function.

Syntax

atanh(x)

Arguments

The atanh function takes the following argument:

x

The hyperbolic tangent of the value that the function returns, as a DOUBLE value greater than -1 and less than 1. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the inverse hyperbolic tangent of 0.5.

SELECT atanh(0.5) AS result

The query returns the following result.

result

0.5493061443340548

The following SQL query uses isinf and isnan to show the results at the edge of the range and outside it. atanh(1) is infinity, and atanh(2) is NaN.

SELECT isinf(atanh(1)) AS is_infinite, isnan(atanh(2)) AS is_nan

The query returns the following result.

is_infinite is_nan

true

true

Considerations

If x is 1 or -1, the result is positive or negative infinity. If x is outside the range from -1 to 1, the result is NaN (not a number). The query does not fail in either case. To find such results, test them with isinf and isnan.

cos function

Returns the cosine of x, where x is an angle in radians. The result is between -1 and 1. If x is NULL, the result is NULL.

To pass an angle in degrees, convert it with radians first.

Syntax

cos(x)

Arguments

The cos function takes the following argument:

x

The angle in radians, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the cosine of 90 radians. Because the argument is in radians, not degrees, the result is not 0.

SELECT cos(90) AS result

The query returns the following result.

result

-0.4480736161291701

The following SQL query converts 60 degrees to radians, returns the cosine, and rounds the result to ten decimal places.

SELECT round(cos(radians(60)), 10) AS result

The query returns the following result.

result

0.5

Considerations

The result is a floating-point approximation. For example, cos(radians(60)) can differ slightly from 0.5. Round the result before you compare it with a constant.

cosh function

Returns the hyperbolic cosine of x, which is (exp(x) + exp(-x)) / 2. The result is always at least 1, and cosh(-x) equals cosh(x). If x is NULL, the result is NULL.

Syntax

cosh(x)

Arguments

The cosh function takes the following argument:

x

The number to take the hyperbolic cosine of, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the hyperbolic cosine of 1.

SELECT cosh(1) AS result

The query returns the following result.

result

1.5430806348152437

The following SQL query shows that the hyperbolic cosine of 1000 is infinity.

SELECT isinf(cosh(1000)) AS is_infinite

The query returns the following result.

is_infinite

true

Considerations

When the result is too large for DOUBLE, as it is for cosh(1000), the function returns infinity, and the query does not fail.

cot function

Returns the cotangent of x, where x is an angle in radians. The cotangent is 1 / tan(x). If x is NULL, the result is NULL.

Syntax

cot(x)

Arguments

The cot function takes the following argument:

x

The angle in radians, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the cotangent of 0.5 radians.

SELECT cot(0.5) AS result

The query returns the following result.

result

1.830487721712452

The following SQL query returns the cotangent of 45 degrees, which is pi() / 4 radians, and rounds the result to ten decimal places.

SELECT round(cot(pi() / 4), 10) AS result

The query returns the following result.

result

1.0

The following SQL query shows that the cotangent of 0 is infinity.

SELECT isinf(cot(0)) AS is_infinite

The query returns the following result.

is_infinite

true

Considerations

cot(0) returns infinity, and the query does not fail. To find such results, test them with isinf.

degrees function

Returns x, an angle in radians, converted to degrees. The result is x multiplied by 180 / pi(). If x is NULL, the result is NULL.

To convert an angle from degrees to radians, use the radians function.

Syntax

degrees(x)

Arguments

The degrees function takes the following argument:

x

The angle in radians, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query converts pi() radians to degrees.

SELECT degrees(pi()) AS result

The query returns the following result.

result

180.0

The following SQL query returns the number of degrees in one radian.

SELECT degrees(1) AS result

The query returns the following result.

result

57.29577951308232

pi function

Returns pi, the ratio of a circle's circumference to its diameter, as a DOUBLE value.

To convert an angle between radians and degrees, use the degrees function and the radians function.

Syntax

pi()

Return type

DOUBLE

Examples

The following SQL query returns pi.

SELECT pi() AS result

The query returns the following result.

result

3.141592653589793

The following SQL query computes the area of a circle with a radius of 2, rounded to four decimal places.

SELECT round(pi() * power(2, 2), 4) AS circle_area

The query returns the following result.

circle_area

12.5664

Considerations

Write the parentheses, as in pi(). Without them, the query reads pi as a column name and fails.

radians function

Returns the angle x, given in degrees, converted to radians. The result is x multiplied by pi and divided by 180. If x is NULL, the result is NULL.

The trigonometric functions, such as sin, cos, and tan, take their argument in radians. Use radians to pass them an angle in degrees. To convert radians to degrees, use the degrees function.

Syntax

radians(x)

Arguments

The radians function takes the following argument:

x

The angle in degrees, as a DOUBLE value. Integer, DECIMAL, and FLOAT values are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query converts 90 degrees to radians. The result is pi divided by 2.

SELECT radians(90) AS result

The query returns the following result.

result

1.5707963267948966

The following SQL query shows that 180 degrees equals pi radians.

SELECT radians(180) = pi() AS is_pi

The query returns the following result.

is_pi

true

sin function

Returns the sine of x, where x is an angle in radians. The result is from -1 to 1. If x is NULL, the result is NULL.

To pass an angle in degrees, convert it with radians first.

Syntax

sin(x)

Arguments

The sin function takes the following argument:

x

The angle in radians, as a DOUBLE value. Integer, DECIMAL, and FLOAT values are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the sine of pi divided by 2, which is a right angle.

SELECT sin(pi() / 2) AS result

The query returns the following result.

result

1.0

The following SQL query passes 90. The argument is in radians, so the result is the sine of 90 radians, not of 90 degrees.

SELECT sin(90) AS result

The query returns the following result.

result

0.8939966636005579

The following SQL query returns the sine of 30 degrees. It converts the angle with radians and rounds the result to 10 decimal places.

SELECT round(sin(radians(30)), 10) AS result

The query returns the following result.

result

0.5

Considerations

The result is a floating-point approximation. For example, sin(radians(30)) returns 0.49999999999999994, not 0.5. To compare or show a fixed number of digits, round the result.

sinh function

Returns the hyperbolic sine of x, which equals (exp(x) - exp(-x)) / 2. If x is NULL, the result is NULL.

Syntax

sinh(x)

Arguments

The sinh function takes the following argument:

x

The number to take the hyperbolic sine of, as a DOUBLE value. Integer, DECIMAL, and FLOAT values are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the hyperbolic sine of 1.

SELECT sinh(1) AS result

The query returns the following result.

result

1.1752011936438014

The following SQL query shows that a large argument returns infinity instead of an error.

SELECT isinf(sinh(1000)) AS is_infinite

The query returns the following result.

is_infinite

true

Considerations

The result grows quickly. For a large x, such as 1000, it is too large for a DOUBLE, and the function returns infinity (negative infinity for a large negative x) instead of an error. To check for this, use the isinf function.

tan function

Returns the tangent of x, where x is an angle in radians. If x is NULL, the result is NULL.

To pass an angle in degrees, convert it with radians first.

Syntax

tan(x)

Arguments

The tan function takes the following argument:

x

The angle in radians, as a DOUBLE value. Integer, DECIMAL, and FLOAT values are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query passes 90. The argument is in radians, so the result is the tangent of 90 radians, not of 90 degrees.

SELECT tan(90) AS result

The query returns the following result.

result

-1.995200412208242

The following SQL query returns the tangent of pi divided by 4 radians, which is 45 degrees. It rounds the result to 10 decimal places.

SELECT round(tan(pi() / 4), 10) AS result

The query returns the following result.

result

1.0

The following SQL query shows that the tangent of pi divided by 2 is a very large number, not an error.

SELECT tan(pi() / 2) > 1e15 AS is_large

The query returns the following result.

is_large

true

Considerations

The tangent is undefined at pi divided by 2 and at every odd multiple of it, but tan does not fail or return infinity there. Because pi() / 2 is not exact in floating point, tan(pi() / 2) returns a very large finite number. The result is about 1.6 times 10 to the power of 16.

tanh function

Returns the hyperbolic tangent of x, which equals sinh(x) / cosh(x). The result is between -1 and 1. For an x far from zero, the result is so close to 1 or -1 that a DOUBLE holds it as exactly 1 or -1. If x is NULL, the result is NULL.

Syntax

tanh(x)

Arguments

The tanh function takes the following argument:

x

The number to take the hyperbolic tangent of, as a DOUBLE value. Integer, DECIMAL, and FLOAT values are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query returns the hyperbolic tangent of 1.

SELECT tanh(1) AS result

The query returns the following result.

result

0.7615941559557649

The following SQL query passes a large argument. The result is exactly 1.

SELECT tanh(100) AS result

The query returns the following result.

result

1.0