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
|
.TH PC 1
.SH NAME
pc \- programmer's calculator
.SH SYNOPSIS
.B pc
[
.B -n
]
.SH DESCRIPTION
.I Pc
is an arbitrary precision integer calculator with a special emphasis on supporting two's complement bit operations and working with different number bases.
.PP
.I Pc
reads input statements which are either expressions or control statements.
Multiple statements in one line can be separated by semicolons.
.I Pc
prints the value of all expressions that are not terminated by a semicolon.
.PP
.I Pc
can be run non-interactively by using the
.B -n
switch. In this case no input prompt is printed.
.PP
Expressions can use the C-like operators
.TP
.B + - * ** \fR(exponentiation\fR)
.TP
.B / % \fR(Euclidean division, by default\fR)
.TP
.B "& | ^ ~ ! << >>"
.TP
.B "&& || \fR(returning the second argument, if appropriate)"
.TP
.B < >= < <= == !=
.PP
The \fB$\fR operator performs sign extension. \fIn\fB$\fIx\fR truncates \fIx\fR to \fIn\fR bits and sign extends.
If \fIn\fR is omitted, it is inferred from the highest set bit (the result is always ≤ 0 in this case).
.PP
Variables can be defined using
.BR = .
The builtin variable
.B @
always refers to the last printed result.
.PP
Numbers can use the prefixes
.B 0b
(binary),
.B 0
(octal),
.B 0d
(decimal) and
.B 0x
(hexadecimal).
.B _
in numbers can be added for readability and is ignored.
.SS Builtin functions
.TF \fIcat(a0,n0,...,aN,nN)
.TP
.I bin(n)
Display \fIn\fR in binary.
.TP
.I oct(n)
Display \fIn\fR in octal.
.TP
.I dec(n)
Display \fIn\fR in decimal.
.TP
.I hex(n)
Display \fIn\fR in hexadecimal.
.TP
.I pb(n, b)
Display \fIn\fR in base \fIb\fR (currently must be one of 0, 2, 8, 10, 16; 0 uses the defined output base).
.TP
.I abs(n)
Absolute value of \fIn\fR.
.TP
.I round(n,m)
\fIn\fR rounded to the nearest multiple of \fIm\fR.
Numbers exactly halfway between are rounded to the next even multiple.
.TP
.I floor(n,m)
\fIn\fR rounded down to the next multiple of \fIm\fR.
.TP
.I ceil(n,m)
\fIn\fR rounded up to the next multiple of \fIm\fR.
.TP
.I trunc(n,m)
\fIn\fR truncated to \fIm\fR bits.
.TP
.I xtend(n,m)
\fIn\fR truncated to \fIm\fR bits, with the highest bit interpreted as a sign bit.
.TP
.I rev(n,m)
\fIn\fR truncated to \fIm\fR bits, with the order of bits reversed.
.TP
.I ubits(n)
The minimum number of bits required to represent \fIn\fR as an unsigned number.
.TP
.I sbits(n)
The minimum number of bits required to represent \fIn\fR as an signed number.
.TP
.I nsa(n)
The number of bits set in \fIn\fR.
.TP
.I cat(a\d\s70\s0\u,n\d\s70\s0\u,...,a\d\s7N\s0\u,n\d\s7N\s0\u)
Truncate each of the \fIa\d\s7i\s0\u\fR arguments to \fIn\d\s7i\s0\u\fR bits and concatenate their binary representation.
.TP
.I gcd(n,m)
The greatest common divisor of \fIn\fR and \fIm\fR.
.TP
.I clog(a,b)
The ceiling of the logarithm of \fIa\fR with respect to base \fIb\fR. \fIb\fR can be omitted, in which case it defaults to 2.
.TP
.I minv(n,m)
The inverse of \fIn\fR mod \fIm\fR.
.TP
.I rand(n)
A random number satisfying 0 ≤ \fIrand(n)\fR < \fIn\fR.
.SS Control statements
.PP
Control statements are always evaluated with default input base 10.
.TP
\fL_\fR \fIn\fR
If \fIn\fR ≠ 0, insert
.B _
in all printed numbers, every
.I n
digits.
.TP
\fL<\fR \fIn\fR
Set the default input base to \fIn\fR (default 10).
The input base can always be overriden by the base prefixes defined above.
.TP
\fL>\fR \fIn\fR
Set the output base to \fIn\fR.
If \fIn\fR = 0 (default), print each number in the base it was input in.
.TP
\fL/\fR 0
Use Euclidean division (default).
\fIa\fR / \fIb\fR is rounded towards ±∞ (opposite sign as \fIb\fR).
\fIa\fR % \fIb\fR is always non-negative.
.TP
\fL/\fR 1
Use truncating division (same as C).
\fIa\fR / \fIb\fR is rounded towards zero.
\fIa\fR % \fIb\fR can be negative.
.TP
\fL\'\fR 1
Enable numbering bits (disable with 0).
If the base is a power of two, print the number of the corresponding bit above each digit.
.SH SOURCE
.B /sys/src/cmd/pc.y
.SH "SEE ALSO"
.IR bc (1),
.IR hoc (1)
.SH BUGS
With the input base set to 16, terms such as
.B ABC
are ambiguous.
They are interpreted as numbers only if there is no function or variable of the same name.
To force interpretation as a number, use the \fL0x\fR prefix.
.PP
Arbitrary bases should be supported, but are not supported by the
.IR mp (2)
string functions.
.SH HISTORY
.I Pc
first appeared in 9front (August, 2016).
|