summaryrefslogtreecommitdiff
path: root/README.md
blob: 528d48efd4fdba53d5510b6863822433b277ef9a (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
# perc

A simple AI-engineered command-line percentage calculator written in Go.

## Installation

```bash
go install codeberg.org/snonux/perc/cmd/perc@latest
```

Or using mage:

```bash
mage install
```

## Usage

`perc` supports various percentage calculation formats and RPN (Reverse Polish Notation) stack calculations.

### Percentage Calculations

#### Calculate X% of Y

```bash
perc 20% of 150
# Output:
# 20.00% of 150.00 = 30.00
#   Steps: (20.00 / 100) * 150.00 = 0.20 * 150.00 = 30.00

perc 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
perc 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
perc 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
perc calc 3 4 +           # 3 + 4 = 7
# → 7

perc calc 3 4 -           # 3 - 4 = -1
# → -1

perc calc 5 6 *           # 5 * 6 = 30
# → 30

perc calc 20 4 /          # 20 / 4 = 5
# → 5

perc calc 2 3 ^           # 2^3 = 8
# → 8

perc calc 10 3 %          # 10 % 3 = 1 (modulo)
# → 1
```

#### Expression Chaining

```bash
perc calc 3 4 + 4 4 - *   # (3+4) * (4-4) = 0
# → 0

perc calc 1 2 + 3 *       # (1+2) * 3 = 9
# → 9
```

#### Variables

```bash
perc calc x 5 =           # Assign x = 5
# → x = 5

perc calc x 5 = x x +     # x + x = 10
# → 10

perc calc pi 3.14159 = pi 2 *  # 2 * π
# → 6.28318

# Note: Variable assignment only works with calc/rpn subcommand:
# perc calc x 5 = x x +  (works)
# perc x 5 =             (won't work in bare mode - use "perc calc x 5 =")
```

#### Variable Management

```bash
perc calc vars            # List all variables
# x = 5

perc calc name d          # Delete variable
# Variable removed

perc calc clear           # Clear all variables
# All variables cleared
```

#### Stack Operations

```bash
perc calc 1 2 3 dup       # Duplicate top value
# → 1 2 3 3

perc calc 1 2 swap        # Swap top two values
# → 2 1

perc calc 1 2 3 pop       # Remove top value
# → 1 2

perc calc 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:
```
perc> rpn 2 3 4 +        # Push 2, 3, 4; add last two
2 7
perc> +                  # Add top two: 2 + 7 = 9
9
perc> 5 *                # Multiply by 5: 9 * 5 = 45
45
```

To show the current stack without modifying it:
```
perc> show               # Show current stack state
45
```

## Hyper Operators

Hyper operators work on all values on the stack simultaneously:

```bash
perc calc 1 2 3 4 5 [+]    # Sum all: 1+2+3+4+5 = 15
# → 15

perc calc 2 3 4 [*]        # Multiply all: 2*3*4 = 24
# → 24

perc calc 10 3 2 [-]       # 10 - 3 - 2 = 5
# → 5

perc calc 100 5 2 [/]      # 100 / 5 / 2 = 10
# → 10

perc calc 2 3 2 [^]        # (2^3)^2 = 64
# → 64

perc calc 100 7 3 [%]      # 100 % 7 % 3 = 2
# → 2
```

## Building

Using mage:

```bash
mage build
```

Or using go directly:

```bash
go build -o perc ./cmd/perc
```

## Testing

```bash
mage test
```

Or for RPN-specific tests:

```bash
mage testRPN
```

## License

See LICENSE file for details.