summaryrefslogtreecommitdiff
path: root/man/1/calc
blob: 8c59e8881c0e965a06be0dd6281d42c742916ba8 (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
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
.TH CALC 1
.SH NAME
calc \- calculator language
.SH SYNOPSIS
.B calc
[
.B -s
] [
.I file
]
.PP
.B calc
[
.B -s
] [
.I expression
]
.SH DESCRIPTION
.I Calc
interprets a simple language for floating-point arithmetic
with Limbo-like syntax and
functions.
.PP
If no
.I file
or
.I expression
is given
.I calc
interprets the standard input.
.PP
.I Calc
input consists of
.I expressions
and
.IR statements .
Expressions are evaluated and their results printed.
Statements, typically assignments and function
definitions, produce no output unless they explicitly call
.IR print .
.PP
Comments begin with # and extend to the end of the line as in Limbo.
.PP
Numbers may have a base specified using C or Limbo syntax.
.PP
Variable names have the usual syntax, including 
.LR _ .
They may be introduced without a declaration and have an initial default value
of 0.0.
.PP
The predefined variable
.B degrees
can be set to specify angles in degrees rather than radians in the trigonometric functions below. It is initially 0 (angles in radians by default).
.PP
The predefined variable
.B printbase
can be set to any integer between 2 and 36 inclusive to specify
the base of all values output.
.PP
The constants
.BR e ,
.BR Pi (π) ,
.BR Phi (φ) ,
.BR Gamma (γ) ,
.BR Infinity (∞) ,
and
.BR Nan (NaN)
are predefined.
.PP
Expressions are formed with these Limbo-like operators, listed by
decreasing precedence.
.TP
.B ! ~ + - ++ --
.TP
.B **
.TP
.B * / % //
.TP
.B + -
.TP
.B << >>
.TP
.B > >= < <= <->
.TP
.B == != -> <-
.TP
.BR & " " " " ↑
.TP
.B ^
.TP
.BR | " " " " ↓
.TP
.B &&
.TP
.B ||
.TP
.B ? :
.TP
.B = := += -= *= /= %= //= &= ^= |= <<= >>=
.TP
.B ,
.PP
If the
.B -s
flag is given, a strict interpretation of the declaration rules are enforced - all variables must be declared and initialized first using the 
.B :=
operator. Otherwise undeclared variables are declared and initialized to 0.0 in the
current scope. In either case, 
.B :=
always forces a new declaration.
.PP
The extra non-Limbo operators are factorial (! when postfix), integer division (//),
conditional (? and :) comma (,), logical equivalence (<->), implication (->), reverse implication (<-), nand (↑) and nor (↓).
.PP
Unary operators, assignment operators, **, ? and : are right associative as usual.
.PP
The comma operator may be replaced by white space in expressions.
.PP
Built in functions are
.BR abs ,
.BR acos ,
.BR acosh ,
.BR asin ,
.BR asinh ,
.BR atan ,
.BR atanh ,
.BR atan2 ,
.BR cbrt ,
.BR ceiling ,
.BR cos ,
.BR cosh ,
.BR erf ,
.BR exp ,
.BR floor ,
.BR frac ,
.BR gamma (Γ) ,
.BR int ,
.BR log ,
.BR log10 ,
.BR max ,
.BR min ,
.BR pow ,
.BR rand ,
.BR round ,
.BR sign ,
.BR sin ,
.BR sinh ,
.BR sqrt ,
.BR tan ,
and
.BR tanh .
.PP
Functions of one argument may be written without brackets:
.sp
.EX
	sin 45
	sqrt 2
.EE
.sp
These behave as unary operators with the highest precedence.
.PP
Sum and product operators are available using sigma (Σ) and pi (Π).
For example
.sp
.EX
	sigma(i = 0, 100, 1/i!)
.EE
.sp
gives the value 2.7182818284590455 .
.PP
Simple definite integration can be done:
.sp
.EX
	integral(x = -1.96, 1.96, exp(-0.5*x*x)/sqrt(2*Pi))
.EE
.sp
outputs 0.9500042096998785 .
∫ may be used in place of integral.
.PP
For the sake of completeness, the derivative of a function at a given
point can be calculated:
.sp
.EX
	differential(x=1, x*x+5*x+6)
.EE
.sp
gives 7.
Δ may be used in place of differential.
.PP
There is limited support for the solution of equations.
For example
.sp
.EX
	solve(x**2-5*x+6==0)
.EE
.sp
outputs the values 2 and 3. The value returned by
.B solve
is the largest of the roots. To specify the variable to solve for, if
ambiguous, simply add it as a second parameter as in, for example,
.sp
.EX
	solve(x**2-5*x+6==y**3+z, x)
.EE
.sp
This will substitute the current values of
.B y
and
.B z
and solve for
.B x.
To tune the solution process, the predefined variables
.B solvelimit
(default value 100) and
.B solvestep
(default value 1) are available.
The former specifies the maximum absolute solution
to search for. The latter
specifies the interval increment to apply when searching
for sign changes.
.PP
.B Print
prints a list of expressions that may include
string constants such as
\fL"hello\en"\f1.\fP
.PP
.B Read
reads in a list of values interactively. The list of variables to assign
these values should follow.
.PP
Other files may be read in using the Limbo include statement.
.PP
Control flow statements are
.BR break ,
.BR continue ,
.BR exit ,
.BR return ,
.BR if - else ,
.BR while ,
.BR do - while ,
and
.BR for ,
with braces for grouping.
.PP
The use of semi-colon and newline is optional.
.PP
Functions are introduced by the keyword
.BR fn .
.SH EXAMPLE
.EX
fn ack(a, b)
{
	n = n+1
	if(a == 0)
		return b+1;
	if(b == 0)
		return ack(a-1, 1);
	return ack(a-1, ack(a, b-1));
}

for(i = 0; i < 4; i++)
	for(j = 0; j < 4; j++){
		n = 0
		print "ack(", i, ",", j, ")=", ack(i, j), "\en"
		print n, " calls", "\en"
	}
.EE
.SH SOURCE
.B /appl/cmd/calc.b
.SH "SEE ALSO"
.IR fc (1),
.IR math-intro (2)