]> git.lizzy.rs Git - plan9front.git/blob - sys/man/1/pc
dd(1): update manpage to match program
[plan9front.git] / sys / man / 1 / pc
1 .TH PC 1
2 .SH NAME
3 pc \- programmer's calculator
4 .SH SYNOPSIS
5 .B pc
6 [
7 .B -n
8 ]
9 .SH DESCRIPTION
10 .I Pc
11 is an arbitrary precision integer calculator with a special emphasis on supporting two's complement bit operations and working with different number bases.
12 .PP
13 .I Pc
14 reads input statements which are either expressions or control statements.
15 Multiple statements in one line can be separated by semicolons.
16 .I Pc
17 prints the value of all expressions that are not terminated by a semicolon.
18 .PP
19 .I Pc
20 can be run non-interactively by using the
21 .B -n
22 switch. In this case no input prompt is printed.
23 .PP
24 Expressions can use the C-like operators
25 .TP
26 .B + - * ** \fR(exponentiation\fR)
27 .TP
28 .B / % \fR(Euclidean division, by default\fR)
29 .TP
30 .B "& | ^ ~ ! << >>"
31 .TP
32 .B "&& || \fR(returning the second argument, if appropriate)"
33 .TP
34 .B < >= < <= == !=
35 .PP
36 The \fB$\fR operator performs sign extension. \fIn\fB$\fIx\fR truncates \fIx\fR to \fIn\fR bits and sign extends.
37 If \fIn\fR is omitted, it is inferred from the highest set bit (the result is always ≤ 0 in this case).
38 .PP
39 Variables can be defined using
40 .BR = .
41 The builtin variable
42 .B @
43 always refers to the last printed result.
44 .PP
45 Numbers can use the prefixes
46 .B 0b
47 (binary), 
48 .B 0
49 (octal),
50 .B 0d
51 (decimal) and
52 .B 0x
53 (hexadecimal).
54 .B _
55 in numbers can be added for readability and is ignored.
56 .SS Builtin functions
57 .TF \fIcat(a0,n0,...,aN,nN)
58 .TP
59 .I bin(n)
60 Display \fIn\fR in binary.
61 .TP
62 .I oct(n)
63 Display \fIn\fR in octal.
64 .TP
65 .I dec(n)
66 Display \fIn\fR in decimal.
67 .TP
68 .I hex(n)
69 Display \fIn\fR in hexadecimal.
70 .TP
71 .I pb(n, b)
72 Display \fIn\fR in base \fIb\fR (currently must be one of 0, 2, 8, 10, 16; 0 uses the defined output base).
73 .TP
74 .I abs(n)
75 Absolute value of \fIn\fR.
76 .TP
77 .I round(n,m)
78 \fIn\fR rounded to the nearest multiple of \fIm\fR.
79 Numbers exactly halfway between are rounded to the next even multiple.
80 .TP
81 .I floor(n,m)
82 \fIn\fR rounded down to the next multiple of \fIm\fR.
83 .TP
84 .I ceil(n,m)
85 \fIn\fR rounded up to the next multiple of \fIm\fR.
86 .TP
87 .I trunc(n,m)
88 \fIn\fR truncated to \fIm\fR bits.
89 .TP
90 .I xtend(n,m)
91 \fIn\fR truncated to \fIm\fR bits, with the highest bit interpreted as a sign bit.
92 .TP
93 .I rev(n,m)
94 \fIn\fR truncated to \fIm\fR bits, with the order of bits reversed.
95 .TP
96 .I ubits(n)
97 The minimum number of bits required to represent \fIn\fR as an unsigned number.
98 .TP
99 .I sbits(n)
100 The minimum number of bits required to represent \fIn\fR as an signed number.
101 .TP
102 .I nsa(n)
103 The number of bits set in \fIn\fR.
104 .TP
105 .I cat(a\d\s70\s0\u,n\d\s70\s0\u,...,a\d\s7N\s0\u,n\d\s7N\s0\u)
106 Truncate each of the \fIa\d\s7i\s0\u\fR arguments to \fIn\d\s7i\s0\u\fR bits and concatenate their binary representation.
107 .TP
108 .I gcd(n,m)
109 The greatest common divisor of \fIn\fR and \fIm\fR.
110 .TP
111 .I clog(a,b)
112 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.
113 .TP
114 .I minv(n,m)
115 The inverse of \fIn\fR mod \fIm\fR.
116 .TP
117 .I rand(n)
118 A random number satisfying 0 ≤ \fIrand(n)\fR < \fIn\fR.
119 .SS Control statements
120 .PP
121 Control statements are always evaluated with default input base 10.
122 .TP
123 \fL_\fR \fIn\fR
124 If \fIn\fR ≠ 0, insert 
125 .B _
126 in all printed numbers, every
127 .I n
128 digits.
129 .TP
130 \fL<\fR \fIn\fR
131 Set the default input base to \fIn\fR (default 10).
132 The input base can always be overriden by the base prefixes defined above.
133 .TP
134 \fL>\fR \fIn\fR
135 Set the output base to \fIn\fR.
136 If \fIn\fR = 0 (default), print each number in the base it was input in.
137 .TP
138 \fL/\fR 0
139 Use Euclidean division (default).
140 \fIa\fR / \fIb\fR is rounded towards ±∞ (opposite sign as \fIb\fR).
141 \fIa\fR % \fIb\fR is always non-negative.
142 .TP
143 \fL/\fR 1
144 Use truncating division (same as C).
145 \fIa\fR / \fIb\fR is rounded towards zero.
146 \fIa\fR % \fIb\fR can be negative.
147 .TP
148 \fL\'\fR 1
149 Enable numbering bits (disable with 0).
150 If the base is a power of two, print the number of the corresponding bit above each digit.
151 .SH SOURCE
152 .B /sys/src/cmd/pc.y
153 .SH "SEE ALSO"
154 .IR bc (1),
155 .IR hoc (1)
156 .SH BUGS
157 With the input base set to 16, terms such as
158 .B ABC
159 are ambiguous.
160 They are interpreted as numbers only if there is no function or variable of the same name.
161 To force interpretation as a number, use the \fL0x\fR prefix.
162 .PP
163 Arbitrary bases should be supported, but are not supported by the
164 .IR mp (2)
165 string functions.
166 .SH HISTORY
167 .I Pc
168 first appeared in 9front (August, 2016).