任意精度の計算ができる calc コマンドを使う

はじめに

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

環境

MacOS Sonoma 14.0
Homebrew 4.1.14

calcのインストール

$ brew install calc
$ 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で使い方を見てみる。

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.
...

長いので途中から省略

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

calc 1/4
$ calc 1/4
0.25

ちょっと複雑な計算

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

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

$calc "sqrt(2)"
1.4142135623730950488

根号は sqrtで表現する。

$calc 27!
10888869450418352160768000000

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

$calc 2^29
536870912

冪乗

$ calc "sin(pi())"
0
$ calc "sin(pi()/2)"
1

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

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

おわりに

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

Hugo で構築されています。
テーマ StackJimmy によって設計されています。