日本語の解説が後半にあります。
What is this program?
The program 'calc' is a scientific calculator written in Ruby programming language. It is run from the command line like this.
$ calc 2*3+4*5
26
$ calc
calc > 0.1-0.2
-0.1
calc > sin(PI/6)
0.49999999999999994 # Float always includes error.
calc > quit
$
Prerequisites
- Linux OS
- Ruby 3.1.2 or later (maybe older version is also OK)
installation
There are two ways.
- Click the 'Code' button, then click 'Download ZIP' in the small dialog.
- Unzip the downloaded Zip file, then a new directory 'calc' will be created.
- Type
gem build s_calcunder the directory 'calc', then the gem files_calc-X.X.X.gemis created. The partX.X.Xis a version number like0.1.4. - Type
gem install s_calc-X.X.X.gem.
Another way is simpler. This gem is published to RubyGems.org. You can install it from RubyGems. Just type:
$ gem install s_calc
Be careful.
The command name and GitHub repository name is calc but the gem name is s_calc.
This is because the name calc has already existed in RubyGems.org.
How to use the program.
- Start the terminal and type
calc. - Then the prompt appears
calc > - Type an expression like
2+3and push Enter key, then the answer5appears. - Type
quit,qorexit, then the program will finish. - You can also use it as a one line calculator like
$ calc "10*20+30".
feature
- Operatores:
+,-,*,/,**(power),-(unary minus),(and) - Functions:
sqrt,sin,cos,tan,asin,acos,atan,exp,ln,log,abs,fact,floor,ceilandround. - The special variable
vkeeps the result of the previous calculation. - Variable can be used.
It is a string of alphabets without the keywords above.
It can be used in any place in the expression.
variable_name = expressionis the syntax to define the variable. A defined variable can be used in an expression.
Functions
sqrt: Square root.sqrt(4) => 2sin:sin(PI) => 1.2246467991473532e-16It is well known that sin(PI) is zero. But Float always includes error.cos:cos(PI) => -1tan:tan(PI/4) => 0.9999999999999999The answer includes error. The exact value is 1.asin: Inverse of the sine function.asin(1) => 1.5707963267948966This is PI/2acos: Inverse of the cosine function.acos(1) => 0atan: Inverse of the tangent function.atan(1) => 0.7853981633974483This is PI/4exp: Exponential function.exp(n)meanseraised to the power of n.exp(1) => 2.718281828459045It is E.ln: Natural logarithm.ln(E) => 1log: Common logarithm or decimal logarithm. The base is 10.log(10) => 1abs: Absolute value.abs(-10) =>10fact: Factorial.fact(10) => 3628800The argument must be non-negative. If it isn't an integer, it will be rounded down to an integer. It must be less than 171 because of the limitation of Float.floor: Floor function.floor(3.45) => 3ceil: Ceiling function.ceil(3.45) => 4round: Rounding function.round(3.45,1) => 3.5, round(-3.45,1) => -3.5The rounding is done away from zero if the argument is at the middle of two candidates.
Racc library
Racc is a Ruby standard library.
It is a parser generator like Yacc, which is a famous parser generator made for the Unix Operating system.
Calc uses Racc.
It makes the library file lib/calc/calc.rb with Racc.
The source file is racc/calc.y.
You can compile it by typing:
$ rake
See racc/doc.md for details.
Rakefile
You can do the following
rake: Compileracc/calc.yand create/updatelib/calc/calc.rb.rake rdoc: Create documents underdocsdirectory.rake test: Run test programs undertestdirectory.
Using Calc as a library
You can use Calc as a library in your Ruby program.
- First, require 'calc'.
- Create an instance of the class
Calc. Let the instance name be 'c'. - Call
c.run(s)wheresis a string of an expression. Then it returns the value of the expression. For example,c.run("1+2")returns 3.0.
The following is a sample code.
require 'calc'
c = Calc.new
s = "1+2"
print "#{c.run(s)}\n" #=> 3.0
License
GPL ver 3 or later. See License.md for details.
これは何のプログラム?
Rubyプログラムcalc.rbは関数電卓です。
コマンドラインから次のように起動できます。
$ calc 2*3+4*5
26
$ calc
calc > 0.1-0.2
-0.1
calc > sin(PI/6)
0.49999999999999994 # 実数計算では誤差が発生します
calc > quit
$
動作条件
- Linux OS
- Ruby バージョン3.1.2以降(おそらく不利バージョンでも動作します)
インストール
2通りのインストール方法があります。
- 'Code'ボタンをクリックし、現れた小さなダイアログの'Download ZIP'をクリックする
- ダウンロードしたZipファイルを解凍する。これにより新しく「calc」ディレクトリが作られ、その中にすべてのファイルが収められる
- 「gem build s_calc」を実行することにより、gemファイル「s_calc-X.X.X.gem」が作られる。「X.X.X」の部分はバージョン番号で、例えば「0.1.4」のような数字である
- 「gem install s_calc-X.X.X.gem」とタイプし、gemをインストールする
もっと簡単なインストール方法は、RubyGemsからインストールすることです。 次のようにタイプするだけでgemをダウンロードしインストールします。
$ gem install s_calc
コマンド名とGitHubのレポジトリ名がcalcであるのに対して、gem名はs_calcであることに注意してください。
これは、RubyGemsには既にcalcという名前のgemが存在しており、異なるgem名が必要だったためです。
使い方
- 解凍したフォルダで端末(コマンドライン)を起動し、「calc」と入力する
- プロンプト「calc >」が現れる
- 式(例えば2+3)と入力し、エンターキーを押すと、答え「5」が表示される
- 「quit」または「q」または「exit」とタイプするとプログラムは終了する
- 引数を付けて一行計算アプリとしても使える。(例)
$ calc "10*20+30"
特長
- 次の演算記号が使える。
+,-,*,/,**(累乗),-(単項マイナス),(,) - 関数:
sqrt,sin,cos,tan,asin,acos,atan,exp,ln,log,abs,fact,floor,ceilandround. - 変数が使える
- calcは直前の計算結果を記憶しており、変数
vでその値を参照できる - 変数はアルファベットからなる文字列で、上記のキーワードを除く。
変数名 = 式により、変数に値を代入することができる。 定義された変数は、式の中で使うことができる
関数
sqrt: 平方根。sqrt(4) => 2sin:sin(PI) => 1.2246467991473532e-16sin(PI)はゼロですが、浮動小数点の計算は常に誤差が生じますcos:cos(PI) => -1tan:tan(PI/4) => 0.9999999999999999答えは誤差を含んでいます。正確には1になりますasin: サインの逆三角関数。asin(1) => 1.5707963267948966これはPI/2ですacos: コサインの逆三角関数。acos(1) => 0atan: タンジェントの逆三角関数。atan(1) => 0.7853981633974483これはPI/4ですexp: 指数関数。exp(n)はeのn乗。exp(1) => 2.718281828459045これはEですln: 自然対数。底がeの対数。ln(E) => 1log: 常用対数。底が10の対数。log(10) => 1abs: 絶対値。abs(-10) =>10fact: 階乗。fact(10) => 3628800引数はゼロ以上。引数が整数でなければ、切り捨てて整数に直す。引数は171より小さくなければならない。答えがFloatの最大値を越えないように制限するため。floor: フロアー関数。小数点以下を切り捨て。floor(3.45) => 3ceil: シーリング関数。小数点以下を切り上げ。ceil(3.45) => 4round: 四捨五入関数。round(3.45,1) => 3.5, round(-3.45,1) => -3.5丸める桁が5である場合は、ゼロから遠い方に丸める。
Racc
RaccはRubyの標準ライブラリーです。
パーサー・ジェネレーターと呼ばれ、Unixオペレーティングシステム上の有名なYaccに似ています。
CalcはRaccを使ってlib/calc/calc.rbを生成しています。
そのソース・ファイルはracc/calc.yです。
コンパイルは次のようにタイプするだけでできます。
$ rake
ドキュメント racc/doc.mdを参照してください。
Rakefile
次のことができます。
rake:racc/calc.yをコンパイルしてlib/calc/calc.rbを生成するrake rdoc:docsディレクトリ以下にドキュメントを生成するrake test:testディレクトリの下にあるテストプログラムを実行する
Calcをライブラリとして使う
CalcをライブラリとしてRubyプログラムの中で使うことができます。
- まず、'calc'をrequireする
Calcクラスのインスタンスを生成する。仮にその名前をcとする- 数式を表す文字列を
sとすると、c.run(s)はその数式を計算した値を返す 例えば、c.run("1+2")は3.0を返す。
以下に簡単なサンプルコードを示します。
require 'calc'
c = Calc.new
s = "1+2"
print "#{c.run(s)}\n" #=> 3.0
ライセンス
Copyright (C) 2022,2023 ToshioCP (関谷 敏雄)
このプログラムは、フリーソフトウェア財団によって発行された「GNU一般公衆利用許諾書」(バージョン3か、希望によってはそれ以降のバージョンのうちどれか)の定める条件の下で再頒布または改変することができる。
このプログラムは有用であることを願って頒布されますが、全くの無保証 です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含め全く存在しません。 詳しくはGNU 一般公衆利用許諾書(英語)、またはその日本語訳GNU 一般公衆利用許諾書の日本語訳をご覧ください。
なお、ライセンスを英語で記したLicense.mdもあります。