web-dev-qa-db-ja.com

コマンドライン式ソルバー?

Linux互換のTTYベースの計算機を探しています。例えば:

_user@Host:~$ calculate
> 2
2
user@Host:~$ calculate
> 8*6-4
44
user@Host:~$ calculate
> 8*(6-4)
16
_

基本的な操作、atan()のようないくつかの組み込み関数、そしておそらくスクリプトによるカスタム関数をサポートするこのようなものはありますか?

8
Lucas Phillips

bc&dc

bcdcは、端末からのアクセスが必要なときによく使用する2つの計算機です。

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

次に、質問を入力できます。

2
2
5+5
10

終わったら、 Ctrl+C

試乗

これらの計算機はかなり機能が豊富です。

スケーリング

scale=5
193 * 1/3
64.33333

方程式

principal=100
ir = 0.05
years = 5
futurevalue = principal * (1 + ir)^years

futurevalue
127.62800

あなたの例

8*6-4
44

8*(6-4)
16

calc

もう少しインタラクティブなものが必要な場合は、 calc があります。

$ calc
C-style arbitrary precision calculator (version 2.12.4.4)
Calc is open software. For license details type:  help copyright
[Type "exit" to exit, or "help" for help.]

; 10+10
20
; 8*6-4
    44
; 8*(6-4)
    16
; 

上/下矢印を使用して過去のコマンドを実行でき、インタラクティブなヘルプもあります。

; help

あなたにこれを与えます:

For more information while running calc, type  help  followed by one of the
following topics:

    topic               description
    -----               -----------
    intro               introduction to calc
    overview            overview of calc
    help                this file

    assoc               using associations
    builtin             builtin functions
    command             top level commands
    config              configuration parameters
    custom              information about the custom builtin interface
    define              how to define functions
    environment         how environment variables effect calc
    errorcodes          calc generated error codes
    expression          expression sequences
    file                using files
    history             command history
    interrupt           how interrupts are handled
    list                using lists
    mat                 using matrices
    ...

参考文献

7
slm

あなたの質問に対する多くの答えがあります...

シェルでできる簡単なもの。

$ echo $((8*(6-4)))
16

専用プログラムとしてbcがあります。

$ echo "8*(6-4)" | bc
16

スクリプトによるカスタム関数?そうですね、シェルスクリプトとbcの両方に、ある意味でそれらがあります。どこまで行きたいかによります。

なぜPythonではないのですか?学ぶのは簡単です。

$ python
>>> from math import atan
>>> 8*(6-4)+atan(0)
16.0
6
frostschutz

zsh

$ autoload zcalc  # best in ~/.zshrc
$ zcalc
1> 8*(6-4)
16
2> $1*2
32
5

昆虫には、Webベースとターミナルベースの両方のバージョンがあります。

insect example usage

4
shark.dp

Maxima CAS

  • コンソールから実行できます(wxMaximaのようなウィンドウバージョンもあります
  • スクリプトをサポートします( Mac拡張子のバッチテキストファイル
  • 多くの組み込み関数があります
 L(t):= exp(%i * t * 2 *%pi);/*角度で順番にパラメータ化された単位円*/
 plot2d(
 [atan2(imagpart(L(x))、realpart(L(x)))]、
 [ x、0,1]、
 [y、-2 *%pi、2 *%pi]、
 [plot_format、gnuplot]、
 [gnuplot_term、 "png"] 、
 [gnuplot_out_file、 "atan2.png"]、
 [legend、 "atan2"]、
 [xlabel、 "angle in radians"]、
 [ ylabel、 "angle in radians"]、
 [gnuplot_preamble、 "
 set key left top; 
 set xtics( 'pi/2'0.25、' pi '0.5、' 3pi/2 '0.75、' 2pi '1.0); 
 set ytics(' -2pi'-6.283、 '-pi'-3.1415、' -pi/2'-1.5708、 '0' 0、 'pi/2 '1.5708、' pi '3.1415、' 2pi '6.283); 
 set grid xtics ytics "] 
); 
 

HTH

2
Adam

これは、コマンドラインで直接簡単な計算を実行できるようにする小さなバッシュハックです。

_alias calc='set -o noglob; docalc'
function docalc { Perl -e "print STDOUT $*, \"\\n\""; set +o noglob; }
_

その後、あなたはすることができます、例えば、

_calc 3 * 15 + 5
_

残念ながら、括弧ではうまく機能しません。 (私が正しく思い出せば、tcshにもそれらを受け入れるようにさせることができますが、bashを説得することはできませんでした。)

PS。 Perl呼び出しをecho $(( $* ))に置き換えることで、算術演算をbashに頼ることができます。しかし、それはあなたに_5/6_などの整数除算を与えるでしょう。Perlの算術はもっと便利です。

代替: pythonの牛肉だけが_import math_する必要がある場合、次はあなたの友達です:

_% alias calc='python -i -c "from math import *"'
% calc
>>> 5 * atan(0.25)
_
1
alexis

もう1つの方法は、Python 3:のようなインタープリターを使用することです。

$ python3
> from math import *
> 213/53*exp(0.8)/asin(3)
9.645423462356044

すべてをスクリプト化できるという利点があります。すでに多くの関数があり(複素数が必要な場合はcmathをインポートすることもできます)、ほとんどの場合、精度を設定する必要はありません。pythonは、ほとんどのLinuxインストールにすでにインストールされています。

Python 3.xは、Python 2.xよりも汎用計算機に適しています。Python 3.xは、結果がそうでない場合に浮動小数点除算を行うためです。 int。

$ python3
> 3/2
1.5
$ python2
> 3/2
1

少なくとも私にとっての主な欠点は、フォーマット文字列がないと、フロートがあまり大きくも小さくもない場合、科学的記数法で印刷されないことです。

> exp(35)
1586013452313430.8
> "{:e}".format(exp(35))
'1.586013e+15'