summaryrefslogtreecommitdiff
path: root/man/1/fc
blob: cf3b543d8bc12ba87ac4746c642b2b04260fa213 (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
.TH FC 1
.SH NAME
fc \- command-line floating point calculator
.SH SYNOPSIS
.B fc
[
.I base
]
.I expression
.br
.SH DESCRIPTION
.B Fc
calculates the result of its argument
.I expression
and prints the result in the format indicated by
the optional base argument.
.I Base
can be one of:
.TP
.B -d
Decimal, as produced by the
.B %g
format of
.IR sys-print (2).
.br
.TP
.B -x
Hexadecimal, prefixed with
.BR 0x .
.TP
.B -o
Octal, prefixed with
.BR 0 .
.TP
.B -b
Binary, prefixed with
.BR 0b .
.TP
.B -B
As
.BR -b ,
but with extra lines to help bit-counting.
.TP
.BI -r\  radix
In base
.IR radix ,
prefixed with
.IB \fR``\fPradix r\fR'',\fP
as understood by Limbo (e.g.
.BR 16r3fff ).
.TP
.B -c
As a unicode character, prefixed with
.BR @ .
.RE
.PP
.I Expression
is in reverse polish notation:
each command line argument is either an operand (number) or an operator.
Operands are pushed on a stack; operators pop items from
the stack (the number of items depends on the operator)
and push their result. All operands are converted to double precision
floating point numbers before being pushed.
Integer operations convert their operands to big (64-bit) integers.
When all arguments are exhausted, all the values currently
on the stack are printed, first-pushed first, in the specified
output format.
.PP
Operands can be given in any of the formats that
.I fc
can print, as detailed above.
.PP
When an operation is not commutative, the argument values
will be taken from the stack first-pushed first.
Most functions from from
.IR math-elem (2),
.IR math-fp(2)
are provided.
In addition, other provided operators include:
.TP
.B + - / x
Representing the four rules. Note the use of
.B x
rather than
.BR * ,
to avoid clashes with shell metacharacters.
.br
.TP
.B xx
To the power. (equivelant to 'pow')
.br
.TP
.B rad deg
Convert value to or from radians.
.br
.TP
!
Factorial.
.br
.TP
.B _
Unary minus.
.br
.TP
.B and or xor not
Bitwise operations.
.br
.TP
.B shl shr
Bitwise shift left/right.
.TP
.B p
Print the current top value on the stack.
.br
.TP
.B sum
Sum all the values currently pushed on the stack.
.br
.TP
.B swap
Swap the top two stack items.
.br
.TP
.B dup
Duplicate the top item on the stack.
.br
.TP
.B rep
Repeatedly execute the last operator until
there is only only one item left on the stack.
This is only valid for operators that take exactly two
arguments.
.RE
.PP
A few symbolic names for operands are recognised, including
.B pi
(or
.BR π ),
.BR e ,
and
.BR macheps .
.SH EXAMPLES
.PP
fc 22 7 /
.PP
.nf
	gives 3.1428571429
.fi
.PP
fc -b 1 2 3 4 sum
.PP
.nf
	gives 0b00001010
.fi
.PP
fc 10 0b10 010 0x10 x rep 0xa00 swap -
.PP
.nf
	gives 0
.fi
.PP
fc -help
.PP
.nf
	gives a usage summary, including a list of
	the names of all the operators.
.fi
.ne 5
.SH SEE ALSO
.IR calc (1),
.IR math-fp (2),
.IR math-elem (2),
.IR sh-expr (1)
.SH DIAGNOSTICS
An error message is displayed if
an operator is called on a stack with
too few elements. This also causes
.I fc
to yield a non-null exit status.
.SH BUGS
The
.B -B
option will only work for fixed-width fonts.