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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# gt
A simple AI-engineered command-line percentage calculator written in Go.
```
┌────────────────────────────────┐
│ G T Calculator │
│ │
│ (o.o) │
│ /| | \ 5 6 * │
│ / | | \ _______ │
│ / | | \ / G T \ │
│ ( | | )% + | │
│ \ | | / | RPN | │
│ \ | | / | Calc | │
│ \|___|/ |_______| │
│ Result: 30 │
└────────────────────────────────┘
```
## Installation
```bash
go install codeberg.org/snonux/perc/cmd/gt@latest
```
Or using mage:
```bash
mage install
```
## Usage
`gt` supports various percentage calculation formats and RPN (Reverse Polish Notation) stack calculations.
### Percentage Calculations
#### Calculate X% of Y
```bash
gt 20% of 150
# Output:
# 20.00% of 150.00 = 30.00
# Steps: (20.00 / 100) * 150.00 = 0.20 * 150.00 = 30.00
gt what is 20% of 150
# Output:
# 20.00% of 150.00 = 30.00
# Steps: (20.00 / 100) * 150.00 = 0.20 * 150.00 = 30.00
```
#### Find what percentage X is of Y
```bash
gt 30 is what % of 150
# Output:
# 30.00 is 20.00% of 150.00
# Steps: (30.00 / 150.00) * 100 = 0.20 * 100 = 20.00%
```
#### Find the whole when X is Y% of it
```bash
gt 30 is 20% of what
# Output:
# 30.00 is 20.00% of 150.00
# Steps: (30.00 / 20.00) * 100 = 1.50 * 100 = 150.00
```
### RPN (Reverse Polish Notation) Calculations
RPN (postfix notation) uses a stack-based approach where operators follow their operands. No parentheses needed!
#### Basic Arithmetic
```bash
gt 3 4 + # 3 + 4 = 7
# → 7
gt 3 4 - # 3 - 4 = -1
# → -1
gt 5 6 * # 5 * 6 = 30
# → 30
gt 20 4 / # 20 / 4 = 5
# → 5
gt 2 3 ^ # 2^3 = 8
# → 8
gt 10 3 % # 10 % 3 = 1 (modulo)
# → 1
```
#### Expression Chaining
```bash
gt 3 4 + 4 4 - * # (3+4) * (4-4) = 0
# → 0
gt 1 2 + 3 * # (1+2) * 3 = 9
# → 9
```
#### Variables
```bash
gt x 5 = # Assign x = 5
# → x = 5
gt x 5 = x x + # x + x = 10
# → 10
gt pi 3.14159 = pi 2 * # 2 * π
# → 6.28318
# Note: Variable assignment works in bare mode (e.g., "gt x 5 =").
# gt x 5 = x x + (works)
```
#### Variable Management
```bash
gt vars # List all variables
# x = 5
gt delete x # Delete a specific variable
# Variable removed
gt clear # Clear all variables
# All variables cleared
```
### Working with Variables
Variables persist across commands in REPL mode but are cleared when exiting. In bare mode (single command), variables are only available within that command's execution context.
Example:
```bash
# In bare mode, variables don't persist between commands
gt x 5 = # Assign x = 5 (in this command only)
# → x = 5
gt x # x is not defined in this separate command
# Error: variable not found
# In REPL mode, variables persist
> x 5 = # Assign x = 5
> x # x is still 5
5
> clear # Clear all variables
> x # x is now undefined
# Error: variable not found
```
#### Stack Operations
```bash
gt 1 2 3 dup # Duplicate top value
# → 1 2 3 3
gt 1 2 swap # Swap top two values
# → 2 1
gt 1 2 3 pop # Remove top value
# → 1 2
gt 1 2 3 show # Show stack without modifying
# → 1 2 3
```
### REPL Mode Notes
In REPL mode, RPN operations maintain persistent state between commands. This allows you to build up values on the stack across multiple commands.
Example REPL session:
```
> 2 3 4 + # Push 2, 3, 4; add last two
2 7
> + # Add top two: 2 + 7 = 9
9
> 5 * # Multiply by 5: 9 * 5 = 45
45
```
To show the current stack without modifying it:
```
> show # Show current stack state
45
```
## Boolean-to-Number Coercion
Boolean values are automatically coerced to numbers when used in arithmetic operations:
- `true` is treated as `1`
- `false` is treated as `0`
This enables mixed boolean-numeric expressions:
```bash
gt 5 3 == 1 + # 5 == 3 is false (0), 0 + 1 = 1
# → 1
gt 0 false + # false is 0, 0 + 0 = 0
# → 0
gt true 2 * # true is 1, 1 * 2 = 2
# → 2
gt 9 3 > 4 5 < + # 9 > 3 is true (1), 4 < 5 is true (1), 1 + 1 = 2
# → 2
```
Note: The boolean result is shown as `true`/`false` when printed, but when used as an operand it behaves as the corresponding numeric value.
## Hyper Operators
Hyper operators work on all values on the stack simultaneously:
```bash
gt 1 2 3 4 5 [+] # Sum all: 1+2+3+4+5 = 15
# → 15
gt 2 3 4 [*] # Multiply all: 2*3*4 = 24
# → 24
gt 10 3 2 [-] # 10 - 3 - 2 = 5
# → 5
gt 100 5 2 [/] # 100 / 5 / 2 = 10
# → 10
gt 2 3 2 [^] # (2^3)^2 = 64
# → 64
gt 100 7 3 [%] # 100 % 7 % 3 = 2
# → 2
```
## Building
Using mage:
```bash
mage build
```
Or using go directly:
```bash
go build -o gt ./cmd/gt
```
## Testing
```bash
mage test
```
Or for RPN-specific tests:
```bash
mage testRPN
```
## Rational Number Mode (Optional)
The calculator supports precise rational number calculations using Go's `*big.Rat` type. By default, calculations use float64 for performance.
### Enabling Rational Mode
In REPL mode, you can switch between float64 and rational number modes:
```
> rat on # Enable rational number mode
Rational mode enabled
> rat off # Disable rational number mode (default)
Rational mode disabled (using float64)
> rat toggle # Switch to the other mode
Rational mode enabled
```
When rational mode is enabled:
- Results are calculated with arbitrary precision
- Output is displayed as a decimal approximation
- Use `rat off` to return to standard float64 calculations
## License
See LICENSE file for details.
|