LoginSignup
0
0

More than 3 years have passed since last update.

Kinx ライブラリ - Math

Posted at

Math

はじめに

「見た目は JavaScript、頭脳(中身)は Ruby、(安定感は AC/DC)」 でお届けしているスクリプト言語 Kinx。言語はライブラリが命。ということでライブラリの使い方編。

今回は Math です。

Math は数値演算ライブラリ。同じメソッドが Integer、Double でも特殊メソッドとして使えるので、あえて Math オブジェクトを通して使う必要はない。例えば、以下のように使用できる。

var a = 2.pow(10);     // Math.pow(2, 10)   => 1024
var b = 2.0.pow(10);   // Math.pow(2.0, 10) => 1024
var c = (-10).abs();   // Math.abs(-10)     => 10

単項マイナス(-)は関数呼び出しより優先順位が低いため、カッコで括る必要があることに注意。何気に random() も数値に対して使えるが、レシーバーの値に意味はない、というのは知っていても良いし、まぁ知らなくても良い。

var a = 1000.random(); // 1000 に意味はない
var b = 10.0.random(); // 10.0 に意味はない
var c = Math.random(); // Math には意味がある

特殊メソッド、および特殊オブジェクトに関する詳細は Kinx ライブラリ - String を参照してください。

Math

Math オブジェクトのメソッド一覧は以下の通り。

メソッド 意味
Math.sin(dbl) サイン。Math.sin(x) は、$\sin x$ を求める。
Math.cos(dbl) コサイン。Math.cos(x) は、$\cos x$ を求める。
Math.tan(dbl) タンジェント。Math.tan(x) は、$\tan x$ を求める。
Math.asin(dbl) アークサイン。Math.asin(x) は、$\sin θ=x$ となるような $θ$ を求める。
Math.acos(dbl) アークコサイン。Math.acos(x) は、$\cos θ=x$ となるような $θ$ を求める。
Math.atan(dbl) アークタンジェント。Math.atan(x) は、$\tan θ=x$ となるような $θ$ を求める。
Math.atan2(dbl, dbl) アークタンジェント2。Math.atan2(x,y) は、$xy$ 直交座標における $(x,y)$ の偏角を求める。
Math.sinh(dbl) ハイパボリックサイン。Math.sinh(x) は、$\sinh x$ を求める。
Math.cosh(dbl) ハイパボリックコサイン。Math.cosh(x) は、$\cosh x$ を求める。
Math.tanh(dbl) ハイパボリックタンジェント。Math.tanh(x) は、$\tanh x$ を求める。
Math.exp(dbl) Math.exp(x) は $e^x$ を返す。$e$ は、自然対数の底であるネイピア数(オイラー数)。
Math.ldexp(dbl, int) Math.ldexp(x,y) は $x \times 2^y$ を返す。
Math.log(dbl) Math.log(x) は $\log_e x$ を返す。
Math.log10(dbl) Math.log10(x) は $\log_{10} x$ を返す。
Math.pow(dbl, dbl) Math.pow(x,y) は $x^y$ を返す。
Math.sqrt(dbl) Math.sqrt(x) は $\sqrt x$ を返す。
Math.ceil(dbl) Math.ceil(x) は $x$ 以上の最小の整数値を Double で返す。
Math.floor(dbl) Math.floor(x) は $x$ 以下の最大の整数値を Double で返す。
Math.fmod(dbl, dbl) Math.fmod(x,y) は $x \div y$ の浮動小数点余剰を返す。結果は $x$ と同じ符号で $y$ 以下の絶対値となる。
Math.abs(dbl) Math.abs(x) は $x$ の絶対値を Double で返す。
Math.random() Math.random() は $0 \leq r < 1$ となる乱数を返す。

おわりに

最初にも書いたが、同じメソッドが Integer、Double に直接作用するので、Math オブジェクト経由であえて使う必要はないかな、と。

ではまた次回。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0