// math reference

SQL inverse trig domain errors

Each engine's reaction to inverse-trig input outside [-1, 1], the ACOSH and ATANH domains, and the LEAST/GREATEST clamp (MAX/MIN on SQLite) that prevents it.

Categoría
Math
Returns
float / NULL / error
In this category
73 of 82

Sintaxis

ASIN(2)
ASIN(LEAST(1, GREATEST(-1, x)))

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Sí Msg 3623: An invalid floating point operation occurred; the statement fails
PostgreSQL Sí ERROR: input is out of range; the statement fails
MySQL Different name or syntax Returns NULL with a warning
SQLite Different name or syntax Returns NULL
Oracle Sí ORA-01428: argument is out of range

Ejemplos

MySQL

SELECT ASIN(2) AS out_of_domain;  -- NULL

PostgreSQL

SELECT ASIN(2) AS boom;  -- ERROR: input is out of range

PostgreSQL

SELECT ASIN(LEAST(1, GREATEST(-1, 2))) AS clamped;  -- 1.5707963267948966

SQLite

SELECT asin(max(-1, min(1, 2))) AS clamped;  -- 1.5707963267949

Conviene saberlo

  • Float error pushes borderline inputs out of domain: a computed cosine of 1.0000000000000002 fails in ACOS on PostgreSQL even though the math says 1.0.
  • The clamp is two functions: ASIN(LEAST(1, GREATEST(-1, x))) on SQL Server 2022+, PostgreSQL, MySQL and Oracle; SQLite has no LEAST/GREATEST, so write ASIN(MAX(-1, MIN(1, x))).
  • ACOSH has a [1, infinity) domain: ACOSH(0.5) errors on PostgreSQL and is NULL on SQLite. ATANH(1) is not an error on either; both return Infinity.

Questions about trig domain

Why does ASIN(2) error on PostgreSQL but return NULL on MySQL?

The engines chose differently: PostgreSQL treats out-of-domain math as a hard error, MySQL as a soft NULL. SQLite also returns NULL; Oracle and SQL Server error like PostgreSQL.

How do I stop ACOS from failing on float noise?

Clamp the input into [-1, 1]: ACOS(LEAST(1, GREATEST(-1, x))), or ACOS(MAX(-1, MIN(1, x))) on SQLite. Computed cosine sums land just outside the domain often enough that this guard is standard practice.

What is the domain of ATANH?

Strictly between -1 and 1; the endpoints correspond to infinity. At exactly 1 or -1, PostgreSQL and SQLite return Infinity or -Infinity rather than failing. Beyond them, PostgreSQL errors and SQLite returns NULL.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.