sign
This page explains how to use the sign function in APL.
Use the sign function in APL to return the sign of a numeric expression: +1 for positive values, 0 for zero, and -1 for negative values.
sign is useful when you care about the direction of a value rather than its magnitude. For example, you can use sign to classify whether a request latency is above or below a baseline, detect whether a metric is trending up or down, or encode deviations as ternary labels for downstream classification.
Usage
Syntax
APL
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
x | real | Yes | A real number. |
Returns
+1ifxis positive.0ifxis zero.-1ifxis negative.
Example
Use sign to classify each request as faster (-1), on-target (0), or slower (+1) than a 200 ms baseline.
Query
Output
| _time | id | req_duration_ms | deviation_sign |
|---|---|---|---|
| 2024-11-14 10:00:00 | user-1 | 450.0 | 1 |
| 2024-11-14 10:01:00 | user-2 | 80.0 | -1 |
| 2024-11-14 10:02:00 | user-3 | 200.0 | 0 |
List of related functions
- abs: Returns the absolute value. Use it to get magnitude when direction from
signisn't enough. - round: Rounds a value. Use it to normalize values before comparing signs.
- not: Reverses a boolean. Use it alongside
signfor boolean-style conditions on direction. - pow: Raises a value to a power. Use
pow(x, 2)together withsign(x)to keep direction while amplifying magnitude. - sqrt: Returns the square root. Use it to compute magnitude, and
signto determine direction, when you need a signed root.