はじめに

任意精度の計算ができる calcコマンドというものがあるのでこれを使ってみる。
任意精度の計算は意外と便利、標準の計算機だと桁が溢れてしまったりするので。

環境

1
2
MacOS Sonoma 14.0
Homebrew 4.1.14

calcのインストール

1
$ brew install calc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ brew install calc
==> Downloading https://ghcr.io/v2/homebrew/core/calc/manifests/2.15.0.1
################################################################################################################### 100.0%
==> Fetching calc
==> Downloading https://ghcr.io/v2/homebrew/core/calc/blobs/sha256:ba9d9081584de319c5176cc217669e12fa880d4f31ddefcc11180e0
################################################################################################################### 100.0%
==> Pouring calc--2.15.0.1.sonoma.bottle.tar.gz
🍺  /usr/local/Cellar/calc/2.15.0.1: 550 files, 5.3MB
==> Running `brew cleanup calc`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

使ってみる

まずは manで使い方を見てみる。

  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
273
274
275
276
277
278
279
280
281
282
283
284
285
calc(1)                                          General Commands Manual                                         calc(1)
 
NAME
       calc - arbitrary precision calculator
 
SYNOPSIS
       calc [-c] [-C] [-d]
            [-D calc_debug[:resource_debug[:user_debug]]]
            [-e] [-f filename] [-h] [-i] [-m mode] [-O]
            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
 
       #!/usr/local/Cellar/calc/2.15.0.1/bin/calc [optional_other_flags ...] -f
 
calc(1)                                          General Commands Manual                                         calc(1)
 
NAME
       calc - arbitrary precision calculator
 
SYNOPSIS
       calc [-c] [-C] [-d]
            [-D calc_debug[:resource_debug[:user_debug]]]
            [-e] [-f filename] [-h] [-i] [-m mode] [-O]
            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
 
       #!/usr/local/Cellar/calc/2.15.0.1/bin/calc [optional_other_flags ...] -f
 
 
DESCRIPTION
       -c     Continue reading command lines even after a scan/parse error has caused the abandonment of a line.  Note
              that this option only deals with scanning and parsing of the calc language.  It does not deal with
              execution or run-time errors.
 
              For example:
 
                   calc read many_errors.cal
 
              will cause calc to abort on the first syntax error, whereas:
 
                   calc -c read many_errors.cal
 
              will cause calc to try to process each line being read despite the scan/parse errors that it encounters.
 
              By default, calc startup resource files are silently ignored if not found.  This flag will report missing
~/workspace/blog-hugo$man calc
calc(1)                                          General Commands Manual                                         calc(1)
 
NAME
       calc - arbitrary precision calculator
 
SYNOPSIS
       calc [-c] [-C] [-d]
            [-D calc_debug[:resource_debug[:user_debug]]]
            [-e] [-f filename] [-h] [-i] [-m mode] [-O]
            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
 
       #!/usr/local/Cellar/calc/2.15.0.1/bin/calc [optional_other_flags ...] -f
 
 
DESCRIPTION
       -c     Continue reading command lines even after a scan/parse error has caused the abandonment of a line.  Note
              that this option only deals with scanning and parsing of the calc language.  It does not deal with
              execution or run-time errors.
 
              For example:
 
                   calc read many_errors.cal
 
              will cause calc to abort on the first syntax error, whereas:
 
                   calc -c read many_errors.cal
 
              will cause calc to try to process each line being read despite the scan/parse errors that it encounters.
 
              By default, calc startup resource files are silently ignored if not found.  This flag will report missing
              startup resource files unless -d is also given.
 
 
       -C     Permit the execution of custom builtin functions.  Without this flag, calling the custom() builtin
              function will simply generate an error.
 
              Use of this flag may cause calc to execute functions that are non-standard and that are not portable.
              Custom builtin functions are disabled by default for this reason.
 
 
       -d     Disable the printing of the opening title.  The printing of resource file debug and informational messages
              is also disabled as if config("resource_debug", 0) had been executed.
 
              For example:
 
                   calc "read qtime; qtime(2)"
 
              will output something like:
 
                   qtime(utc_hr_offset) defined
                   It's nearly ten past six.
 
              whereas:
 
                   calc -d "read qtime; qtime(2)"
 
              will just print:
 
                   It's nearly ten past six.
 
              This flag disables the reporting of missing calc startup resource files.
 
 
              This flag also disables the printing the leading tilde. For example:
 
                   calc 2/3
 
              will print:
 
                   ~0.66666666666666666667
 
 
              whereas:
 
                   calc -d 2/3
 
              will just print:
 
                   0.66666666666666666667
 
~/workspace/blog-hugo$man calc
calc(1)                                          General Commands Manual                                         calc(1)
 
NAME
       calc - arbitrary precision calculator
 
SYNOPSIS
       calc [-c] [-C] [-d]
            [-D calc_debug[:resource_debug[:user_debug]]]
            [-e] [-f filename] [-h] [-i] [-m mode] [-O]
            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
 
       #!/usr/local/Cellar/calc/2.15.0.1/bin/calc [optional_other_flags ...] -f
 
 
DESCRIPTION
       -c     Continue reading command lines even after a scan/parse error has caused the abandonment of a line.  Note
              that this option only deals with scanning and parsing of the calc language.  It does not deal with
              execution or run-time errors.
 
              For example:
 
                   calc read many_errors.cal
 
              will cause calc to abort on the first syntax error, whereas:
 
                   calc -c read many_errors.cal
 
              will cause calc to try to process each line being read despite the scan/parse errors that it encounters.
 
              By default, calc startup resource files are silently ignored if not found.  This flag will report missing
              startup resource files unless -d is also given.
 
 
       -C     Permit the execution of custom builtin functions.  Without this flag, calling the custom() builtin
              function will simply generate an error.
 
              Use of this flag may cause calc to execute functions that are non-standard and that are not portable.
              Custom builtin functions are disabled by default for this reason.
 
 
       -d     Disable the printing of the opening title.  The printing of resource file debug and informational messages
              is also disabled as if config("resource_debug", 0) had been executed.
 
              For example:
 
                   calc "read qtime; qtime(2)"
 
              will output something like:
 
                   qtime(utc_hr_offset) defined
                   It's nearly ten past six.
 
              whereas:
 
                   calc -d "read qtime; qtime(2)"
 
              will just print:
 
                   It's nearly ten past six.
 
              This flag disables the reporting of missing calc startup resource files.
 
 
              This flag also disables the printing the leading tilde. For example:
 
                   calc 2/3
 
              will print:
 
                   ~0.66666666666666666667
 
 
              whereas:
 
                   calc -d 2/3
 
              will just print:
 
                   0.66666666666666666667
 
 
 
       -D calc_debug[:resource_debug[:user_debug]]
              Force the initial value of config("calc_debug"), config("resource_debug") and config("user_debug").
 
              The : separated strings are interpreted as signed 32 bit integers.  After an optional leading sign a
              leading zero indicates octal conversion, and a leading ``0x'' or ``0X'' hexadecimal conversion.
              Otherwise, decimal conversion is assumed.
 
              By default, calc_debug is 0, resource_debug is 3 and user_debug is 0.
 
              For more information use the following calc command:
 
                   help config
 
 
       -e     Ignore any environment variables on startup.  The getenv() builtin will still return values, however.
 
 
       -f filename
              This flag is normally only with calc shell scripts.
 
              This flag is required when using calc in shell script mode.  It must be at the end of the initial #!  line
              of the script, as in:
 
                   #!/usr/local/Cellar/calc/2.15.0.1/bin/calc [optional_other_flags ...] -f
 
              the rest of the file will be processed in shell script mode.
 
 
              A common flag to use, prior to the -f on the #! line is the -q flag.  For example:
 
                   #!/usr/local/Cellar/calc/2.15.0.1/bin/calc -q -f
 
 
              See SHELL SCRIPT MODE section of this man page below for details.
 
              While the actual form of this flag is:
 
                   -f filename
 
              for systems that treat an executable that begins with #!  as a script, the path of the executable is
              appended by the kernel as the final argument to the exec() system call.  This is why the -f flag at the
              very end of the #!  line.
 
              It is possible use -f filename on the command line:
 
                   calc [optional_other_flags ...] -f filename
 
              This will cause calc to process lines in filename in shell script mode.
 
 
              NOTE: The use of -f does NOT imply -q and thus one would need to use
 
                   calc [optional_other_flags ...] -q -f filename
 
              to disable the use of calc startup files as well.
 
 
              The use of -f filename implies the -s flag.
 
 
       -h     Print a help message.  This option implies -q.  This is equivalent to the calc command help help.  The
              help facility is disabled unless the mode is 5 or 7.  See -m.
 
 
       -i     Become interactive if possible.  This flag will cause calc to drop into interactive mode after the
              calc_cmd arguments on the command line are evaluated.  Without this flag, calc will exit after they are
              evaluated.
 
              For example:
 
                   calc 2+5
 
              will print the value 7 and exit whereas:
 
                   calc -i 2+5
 
              will print the value 7 and prompt the user for more calc commands.
...

長いので途中から省略

基本は下記で計算できるみたい。

1
calc 1/4
1
2
$ calc 1/4
0.25

ちょっと複雑な計算

1
2
$calc 1/4+1/2+5
5.75
1
2
$calc "1+2-3*4"
-9

アスタリスクは正規表現で使うので、✖️の意味で使う場合は"“で囲む。

1
2
$calc "sqrt(2)"
1.4142135623730950488

根号は sqrtで表現する。

1
2
$calc 27!
10888869450418352160768000000

階乗は!
結構すぐ計算される

1
2
$calc 2^29
536870912

冪乗

1
2
3
4
5
6
7
8
9
$ calc "sin(pi())"
0
$ calc "sin(pi()/2)"
1

$ calc "cos(pi())"
-1
$ calc "cos(pi()/2)"
0

定義などは calc --help defineで見られる。
その他 calc --help で他の情報もあるので、必要であれば参照する。

おわりに

標準の電卓であればできない部分もやってくれるので結構便利そう。
さっと計算したいときにコマンド覚えておくと役に立つかもしれない。