sqrt
sqrt is useful whenever you want to compress large numeric ranges, compute root-mean-square values, normalize metrics, or undo a squared transformation. It's equivalent to pow(x, 0.5) but is more concise for the square-root case.
If the input is negative, sqrt returns NaN (Not a Number). APL represents NaN as null in query results. Use isnan to filter these values before aggregating or charting.
Usage#
Syntax#
sqrt(x)Parameters#
| Name | Type | Required | Description |
|---|---|---|---|
x |
real | Yes | The value to compute the square root of. Must be non-negative for a defined result. |
Returns#
Returns the square root of x.
If x is negative, the function returns NaN, which APL represents as null in query results. Use isnan to check for this condition.
Examples#
Compute the square root of request duration#
Use sqrt to normalize request durations by taking their square root, compressing the range of large values.
Query
['sample-http-logs']
| extend root_duration = sqrt(req_duration_ms)
| project _time, id, req_duration_ms, root_duration
| order by root_duration descOutput
| _time | id | req_duration_ms | root_duration |
|---|---|---|---|
| 2024-11-14 10:00:00 | user-1 | 1600.0 | 40.0 |
| 2024-11-14 10:01:00 | user-2 | 400.0 | 20.0 |
| 2024-11-14 10:02:00 | user-3 | 25.0 | 5.0 |
Detect NaN from negative inputs#
When input values are negative, sqrt returns NaN. APL displays NaN as null. Use isnan to identify and handle these rows.
Query
['sample-http-logs']
| extend shifted = req_duration_ms - 500
| extend root_shifted = sqrt(shifted)
| extend is_invalid = isnan(root_shifted)
| project _time, id, shifted, root_shifted, is_invalidOutput
| _time | id | shifted | root_shifted | is_invalid |
|---|---|---|---|---|
| 2024-11-14 10:00:00 | user-1 | 700.0 | 26.46 | false |
| 2024-11-14 10:01:00 | user-2 | -200.0 | null | true |
| 2024-11-14 10:03:00 | user-3 | -50.0 | null | true |
List of related functions#
- pow: Raises a value to an arbitrary power.
sqrt(x)is equivalent topow(x, 0.5). - isnan: Returns
truewhen a value is NaN. Use it to detectnullresults fromsqrton negative inputs. - abs: Returns the absolute value. Use it to ensure inputs are non-negative before passing them to
sqrt. - exp: Returns e^x. Use it when the inverse operation of
logis needed rather than a root. - log: Returns the natural logarithm. Use it alongside
sqrtwhen working in log space.
Other query languages#
Splunk SPL users
In Splunk SPL, sqrt() works identically: it takes a single numeric argument and returns its square root.
Splunk example
| eval root_duration = sqrt(req_duration_ms)APL equivalent
['sample-http-logs']
| extend root_duration = sqrt(req_duration_ms)ANSI SQL users
In ANSI SQL, SQRT() is a standard built-in function with the same semantics as in APL.
SQL example
SELECT SQRT(req_duration_ms) AS root_duration FROM logsAPL equivalent
['sample-http-logs']
| extend root_duration = sqrt(req_duration_ms)