summaryrefslogtreecommitdiff
path: root/docs/constants.md
blob: 264e2cd676ed2e4b7947650a4efe6faa15ce54ed (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
# Constants

gt includes these built-in mathematical constants. They're resolved automatically — just use the name as a token.

## Built-in Constants

### Fundamental Constants

| Constant | Value | Description |
|----------|-------|-------------|
| `pi` / `π` | 3.141592654 | Ratio of circle circumference to diameter |
| `tau` / `τ` | 6.283185307 | Full circle (2π) |
| `e` / `euler` | 2.718281828 | Base of natural logarithm |
| `phi` / `φ` | 1.618033989 | Golden ratio |

### Square Roots

| Constant | Value | Description |
|----------|-------|-------------|
| `sqrt2` / `√2` | 1.414213562 | √2 |
| `sqrt3` / `√3` | 1.732050808 | √3 |
| `sqrt5` / `√5` | 2.236067977 | √5 |

### Logarithms

| Constant | Value | Description |
|----------|-------|-------------|
| `ln2` / `log2` | 0.6931471806 | Natural log of 2 |
| `ln10` / `log10` | 2.302585093 | Natural log of 10 |
| `log_e` | 0.4342944819 | Log base e of 10 |
| `log_e10` | 0.4342944819 | Same as log_e |

### Reciprocals

| Constant | Value | Description |
|----------|-------|-------------|
| `1/e` / `inv_e` | 0.3678794412 | Reciprocal of e |
| `1/π` / `inv_pi` | 0.3183098862 | Reciprocal of π |

### Special Values

| Constant | Value | Description |
|----------|-------|-------------|
| `inf` / `infinity` | +Inf | Positive infinity |
| `-inf` / `-infinity` | -Inf | Negative infinity |
| `nan` | NaN | Not a Number |

## Usage

Constants are used directly as tokens in RPN expressions:

```bash
gt 'pi'              # → 3.141592654
gt 'pi 2 *'          # → 6.283185307  (2π)
gt 'e phi +'         # → 4.336315817  (e + φ)
gt 'sqrt2 2 ^'       # → 2  ((√2)²)
gt 'tau 4 /'         # → 1.570796327  (π/2)
```

Greek letter aliases work identically:

```bash
gt 'π'               # → 3.141592654
gt 'φ 2 *'           # → 3.236067977
gt '√2 √3 *'         # → 2.449489743  (√6)
```

## Commands

### List all constants

```bash
gt 'constants'
```

Lists all 36 built-in constants with their values, sorted alphabetically.

### Clear user-defined constants

```bash
gt 'clearconstants'
```

Removes all user-defined constants and resets built-in constants to their default values. Built-in constants can't be permanently deleted.

## Examples

### Geometry

```bash
gt 'pi 5 5 * *'           # Circle area: π × 5² = 78.54
gt '2 pi 5 *'             # Circle circumference: 2π × 5 = 31.42
gt '4 3 pi 5 5 5 * * * /' # Sphere volume: 4/3 π r³ = 523.6
```

### Engineering

```bash
gt 'e 1 -'                # 1/e ≈ 0.632 (RC circuit time constant)
gt 'sqrt2 1000 *'         # RMS voltage: √2 × 1000 = 1414.2
gt 'phi 10 *'             # Golden rectangle: φ × 10 = 16.18
```

### Information Theory

```bash
gt 'ln2'                  # ≈ 0.693 (bits to nats conversion)
gt '2 ln2 /'              # ≈ 1.443 (nats to bits)
```

## Edge Cases

### Infinity arithmetic

```bash
gt 'inf 5 +'              # → +Inf
gt 'inf inf +'            # → +Inf
gt 'inf -inf +'           # → NaN
```

### NaN propagation

```bash
gt 'nan 5 +'              # → NaN
gt '0 0 /'                # → NaN (if supported)
```

### Variable shadowing

If you assign a variable with the same name as a constant, the variable takes precedence:

```bash
gt 'pi 3 = pi'           # → 3  (variable shadows constant)
gt 'clear'               # Clear variables
gt 'pi'                  # → 3.141592654  (constant restored)
```

## Notes

- Constants are resolved before variables in the lookup order
- All constants use float64 precision (10 significant digits in output)
- Greek letter aliases are fully supported for the most common constants
- Built-in constants can't be individually deleted, only overwritten with variables