独学Ruby - 基礎(Numericクラス)
数値クラス(Numeric)
- Numeric
- Integer
- Fixnum
- Bignum
- Float
- Rational (有理数)
- Complex
- Integer
# Rational(分子,分母)形式 require "rational" r = Rational(2,5) + Rational(1,3) p r p r.to_f # Complex(実部, 虚部)形式 require "complex" c = Complex(1, 0.2) p c
数値リテラル
リテラル | 意味 |
---|---|
123 | 整数の10進表記 |
0o123 | 整数の8進表記 |
0d123 | 整数の10進表記 |
0x123 | 整数の16進表記 |
?a | 文字aのASCIIコード(97) |
123.45 | 浮動小数点数 |
1.23e4 | 1.23 x 10の4乗 |
1.23e-4 | 1.23 x 10の-4乗 |
?\t | タブのASCIIコード(9) |
など
数値リテラルのアンダースコアは無視される。
123456 = 123_456 ... 見やすくするための区切り文字として使える
算術演算
演算子 | 演算 |
---|---|
+ | 加算 |
- | 減算 |
* | 乗算 |
/ | 除算 |
% | 剰余(あまり) |
** | べき乗 |
見慣れないのは、べき乗「**」くらいかな。
x.divmod(y)メソッド
/ や % 以外にもdivmodメソッドが使える。
divmodは商と余りを配列にする。
ans = x.divmode(y)のとき、x = ans[0] * y + ans[1]が成立。
# divmodメソッド ans = 10.divmod(3) print ans , "=>" , "ans[0]=", ans[0] , " ans[1]=", ans[1] , "\n" ans = -10.divmod(3) print ans , "=>" , "ans[0]=", ans[0] , " ans[1]=", ans[1] , "\n"
[3, 1]=>ans[0]=3 ans[1]=1 [-4, 2]=>ans[0]=-4 ans[1]=2
xx.modulo(y)メソッド
x % yと同じ。
# moduloメソッド ans = 10 % 3 p ans ans = 10.modulo(3) p ans ans = -10 % 3 p ans ans = -10.modulo(3) p ans
1 1 2 2
x.remainder(y)メソッド
moduloと似ているが得られる値の符号はxの符号に一致する。
# remainderメソッド ans = 10.remainder(3) p ans ans = -10.remainder(3) p ans
1
-1
Mathモジュール
三角関数や対数関数などの演算モジュールが提供されている。
# Mathモジュール名を明示して使う f = 2 p Math.sqrt(f) # Mathモジュールをインクルードして使う include Math p sqrt(f)
メソッド各種(一部)
- cos
- sin
- tan
- log (自然対数 底e)
- log2 (底2)
- log10 (常用対数 底10)
- sqrt など
定数
- PI = 3.141592 ...
- E = 2.718...
数値型の変換
メソッド名 | 意味 |
---|---|
to_f | Floatオブジェクトへの変換 |
to_i | Integerオブジェクトへの変換 |
round | 小数点以下を四捨五入 |
ceil | レシーバより大きくて最も小さい整数を返す |
floor | レシーバより小さくて最も大きい整数を返す |
to_r | Rationalオブジェクトへの変換 |
to_i | Complexオブジェクトへの変換 |
# Integer -> Float p 10.to_f # Float -> Integer p 10.2.to_i # roundメソッドで四捨五入 p 10.2.round # ceilメソッドでレシーバより大きくて最も小さい整数 p 10.2.ceil # floorメソッドでレシーバより小さくて最も大きい整数 p 10.2.floor # Rationalへの変換 p 1.5.to_r # Complexへの変換 p 1.5.to_c
実行結果。
10.0 10 10 11 10 (3/2) (1.5+0i)
ビット演算
演算子 | 演算 |
---|---|
~ | ビット反転(NOT) |
& | 積(AND) |
| | 和(OR) |
^ | 排他的論理和(XOR) |
>> | 右ビットシフト |
<< | 左ビットシフト |
乱数
- rand
- srand
- (Randomモジュールとしても提供されている)
# randメソッド p rand p rand p rand(100) p rand(100) # srandメソッドで擬似乱数初期化 srand(1) p [rand, rand, rand] srand(1) p [rand, rand, rand] srand(2) p [rand, rand, rand] p Random.rand p Random.srand(1) p [rand, rand, rand]
0.9308480165924842 0.46835315268923006 21 0 [0.417022004702574, 0.7203244934421581, 0.00011437481734488664] [0.417022004702574, 0.7203244934421581, 0.00011437481734488664] [0.43599490214200376, 0.025926231827891333, 0.5496624778787091] 0.4353223926182769 2 [0.417022004702574, 0.7203244934421581, 0.00011437481734488664]
数え上げ
- n.times{|i}...}
# timesメソッド ary = [] 10.times do |i| ary << i end p ary
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- from.upto(to){|i}...}
# uptoメソッド ary = [] 1.upto(10) do |i| ary << i end p ary
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- from.downto(to){|i|...}
# downtoメソッド ary = [] 10.downto(1) do |i| ary << i end p ary
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
- from.step(to, step){|i|...}
# stepメソッド ary = [] 1.step(10, 0.5) do |i| ary << i end p ary
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0]
以上。