summaryrefslogtreecommitdiff
path: root/docs/comparisons.md
blob: 0f82ec213b66cd748a42f4bbe97583187f4706cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Comparison RPN Operators

gt provides six comparison operators, each with both a named and a symbolic alias. All operators are metric-aware — they convert values to base units before comparing. Results are boolean (`true` / `false`) and coerce into arithmetic (true → 1, false → 0).

## Operators

| Operator | Alias | RPN         | Infix   | Description     |
|----------|-------|-------------|---------|-----------------|
| `gt`     | `>`   | `a b gt`    | `a > b` | Greater than    |
| `lt`     | `<`   | `a b lt`    | `a < b` | Less than       |
| `gte`    | `>=`  | `a b gte`   | `a >= b`| Greater/equal   |
| `lte`    | `<=`  | `a b lte`   | `a <= b`| Less/equal      |
| `eq`     | `==`  | `a b eq`    | `a == b`| Equal           |
| `neq`    | `!=`  | `a b neq`   | `a != b`| Not equal       |

All operators pop two values (`a` first, then `b`) from the stack, compare them, and push the boolean result.

## Truth Table

### `gt` / `>`

| a   | b   | Result |
|-----|-----|--------|
| 5   | 3   | true   |
| 3   | 5   | false  |
| 5   | 5   | false  |

```
$ gt '5 3 gt'
true
$ gt '5 3 >'
true
```

### `lt` / `<`

| a   | b   | Result |
|-----|-----|--------|
| 3   | 5   | true   |
| 5   | 3   | false  |
| 5   | 5   | false  |

### `gte` / `>=`

| a   | b   | Result |
|-----|-----|--------|
| 5   | 3   | true   |
| 5   | 5   | true   |
| 3   | 5   | false  |

### `lte` / `<=`

| a   | b   | Result |
|-----|-----|--------|
| 3   | 5   | true   |
| 5   | 5   | true   |
| 5   | 3   | false  |

### `eq` / `==`

| a   | b   | Result |
|-----|-----|--------|
| 5   | 5   | true   |
| 5   | 3   | false  |

### `neq` / `!=`

| a   | b   | Result |
|-----|-----|--------|
| 5   | 3   | true   |
| 5   | 5   | false  |

## Metric-Aware Comparison

Comparison operators convert operands to base units before comparing, so different units within the same category can be compared directly.

### Same category, different units

```
$ gt '1km 1000m eq'
true
$ gt '1km 500m gt'
true
$ gt '500m 1km lt'
true
```

### Data storage (decimal SI prefixes)

```
$ gt '1GB 1000MB gte'
true
$ gt '1GB 1000MB gt'
false
$ gt '1GB 1024MB eq'
false
```

`1GB` equals exactly `1000MB` (decimal SI), so `gt` is false and `gte` is true. `1024MB` is not equal to `1GB`.

### Network throughput

```
$ gt '100Mbps 50Mbps gt'
true
$ gt '1Gbps 1000Mbps eq'
true
```

### Incompatible categories

Comparing values from different metric categories produces an error:

```
$ gt '5m 3kg gt'
Error: incompatible metric categories
```

Unitless numbers are always compatible with any metric value.

## Boolean Coercion in Arithmetic

Boolean results coerce to numbers: `true` → 1, `false` → 0. This lets comparison results flow directly into arithmetic.

### Conditional addition

```
$ gt '5 3 gt 1 +'
2
```

`5 > 3` is true (1), so `1 + 1 = 2`.

```
$ gt '3 5 gt 1 +'
1
```

`3 > 5` is false (0), so `0 + 1 = 1`.

### Conditional multiplication

```
$ gt '5 3 gt 10 *'
10
$ gt '3 5 gt 10 *'
0
```

### Chained comparisons

```
$ gt '9 3 gt 4 5 lt +'
2
```

`9 > 3` (true/1) + `4 < 5` (true/1) = `1 + 1 = 2`.

### Boolean arithmetic with subtraction

```
$ gt '5 3 gt 0 -'
1
$ gt '5 5 eq 1 -'
0
```

## Examples

### Threshold checks

```
$ gt '85 80 gt'
true
```

CPU at 85%, threshold at 80% — alert.

### Range validation

Check that a value falls within acceptable bounds:

```
$ gt '72 68 gte 100 lte +'
2
```

Temperature 72°F: `72 >= 68` (true/1) + `72 <= 100` (true/1) = 2 (both checks pass).

```
$ gt '105 68 gte 100 lte +'
1
```

Temperature 105°F: `105 >= 68` (true/1) + `105 <= 100` (false/0) = 1 (outside range).

When the sum is 2, the value is within range. Any other result means at least one check failed.

### Metric validation

```
$ gt '100Mbps 50Mbps gt'
true
```

Download speed 100Mbps exceeds minimum 50Mbps — pass.

### Counting conditions

Sum boolean results to count how many conditions are met:

```
$ gt '10 5 gt 20 15 gt 30 25 gt +'
3
```

All three conditions are true: `1 + 1 + 1 = 3`.

### Guard expressions

Multiply a value by a condition to guard it:

```
$ gt '99 80 gt *'
99
```

Value 99 is used because `99 > 80` is true (1): `99 * 1 = 99`.

```
$ gt '50 80 gt *'
0
```

Value 50 is discarded because `50 > 80` is false (0): `50 * 0 = 0`.

## Edge Cases

### Zero comparisons

```
$ gt '0 0 eq'
true
$ gt '0 0 neq'
false
$ gt '0 1 lt'
true
```

### Negative numbers

```
$ gt '-1 0 lt'
true
$ gt '-5 -3 lt'
true
```

### Insufficient operands

Any comparison on an empty or single-element stack returns an error:

```
$ gt 'gt'
Error: stack is empty
$ gt '5 gt'
Error: stack has insufficient operands
```