Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > rationalライブラリ > Rationalクラス

class Rational

クラスの継承リスト: Rational < Numeric < Comparable < Object < Kernel

要約

有理数を扱うクラスです。

「1/3」のような有理数を扱う事ができます。IntegerFloat と同様に Rational.new ではなく、 Kernel.#Rational を使用して Rational オブジェクトを作成します。

Integer < Rational < Float の順に強いです。つまり other が Float なら、 self を Float に変換してから演算子を適用します。other が Integer なら other を Rational に変換してから演算子を適用します。冪乗は例外です。

特異メソッド

new!(num, den = 1) -> Rational

Rational オブジェクトを生成します。

[PARAM] num:
分子を指定します。
[PARAM] den:
分母を指定します。省略した場合は 1 です。

Kernel#Rational とは異なり、約分していない Rational オブジェ クトを返します。

例:

Rational.new!(1, 3)         # => Rational(1, 3)
Rational.new!(2, 6)         # => Rational(2, 6)

また、引数のチェックも行われません。

例:

Rational.new!(1, 0)         # => Rational(1, 0)
Rational(1, 0)              # => ZeroDivisionError

注意:

Rational.new! は 1.9系 では廃止されました。Kernel.#Rational の方 を使用してください。

# 1.9.1 の場合
Rational.new!(1, 3)  # => NoMethodError
reduce(num, den = 1) -> Rational

約分された Rational オブジェクトを生成します。

[PARAM] num:
分子を指定します。
[PARAM] den:
分母を指定します。省略した場合は 1 です。
[EXCEPTION] ZeroDivisionError:
den に 0 を指定した場合に発生します。

引数 num、den の両方を指定した場合、num/den を既約になるまで約分した Rational オブジェクトを返します。

Kernel#Rational とは異なり、num と den には整数しか指定できません。

例:

Rational.reduce(2, 6)              # => Rational(1, 3)
Rational.reduce(Rational(1, 3), 1) # => NoMethodError: undefined method `gcd' for Rational(1, 3):Rational

注意:

Rational.reduce は 1.9 系 では廃止されました。Kernel.#Rational の 方を使用してください。

# 1.9.1 の場合
Rational.reduce(2, 6)  # => NoMethodError

インスタンスメソッド

self % other -> Rational | Float

剰余を計算します。絶対値が self の絶対値を越えない、符号が self と同じ Numeric を返します。

[PARAM] other:
自身を割る数

例:

Rational(3, 4) % 2               # => Rational(3, 4)
Rational(3, 4) % Rational(2, 1)  # => Rational(3, 4)
Rational(3, 4) % 2.0             # => 0.75
self * other -> Rational | Float

積を計算します。

[PARAM] other:
自身に掛ける数

other に Float を指定した場合は、計算結果を Float で返しま す。

例:

Rational(3, 4) * 2              # => Rational(3, 2)
Rational(3, 4) * 4              # => Rational(3, 1)
Rational(3, 4) * 0.5            # => 0.375
Rational(3, 4) * Rational(1, 2) # => Rational(3, 8)

other に 0 を指定した場合も Rational を返します。

Rational(3, 4) * 0              # => Rational(0, 1)
self ** rhs -> Numeric [redefined by mathn]

[TODO]

self のべき乗を返します。 Rational になるようであれば Rational で返します。

self ** other -> Rational | Float

冪(べき)乗を計算します。

[PARAM] other:
自身を other 乗する数

other に整数を指定した場合は、計算結果を Rational で返します。 other に整数以外を指定した場合は計算結果を Float で返します。

例:

Rational(3, 4) ** 2              # => Rational(9, 16)
Rational(3, 4) ** Rational(2, 1) # => 0.5625
Rational(3, 4) ** 2.0            # => 0.5625

注意:

1.9 系とは計算結果のオブジェクトが異なる場合がある事に注意してください。 other に Rational を指定した場合には戻り値が Rational を返 す場合があります。

# 1.9.1 の場合
Rational(3, 4) ** Rational(2, 1)  # => (9/16)
self + other -> Rational | Float

和を計算します。

[PARAM] other:
自身に足す数

other に Float を指定した場合は、計算結果を Float で返しま す。

例:

Rational(3, 4) + 2               # => Rational(11, 4)
Rational(3, 4) + Rational(2, 1)  # => Rational(11, 4)
Rational(3, 4) + 2.0             # => 2.75
self - other -> Rational | Float

差を計算します。

[PARAM] other:
自身から引く数

other に Float を指定した場合は、計算結果を Float で返しま す。

例:

Rational(3, 4) - 1   # => Rational(-1, 4)
Rational(3, 4) - 0.5 # => 0.25
self / other -> Rational | Float

商を計算します。

[PARAM] other:
自身を割る数

other に Float を指定した場合は、計算結果を Float で返しま す。

例:

Rational(3, 4) / 2              # => Rational(3, 8)
Rational(3, 4) / Rational(2, 1) # => Rational(3, 8)
Rational(3, 4) / 2.0            # => 0.375
Rational(3, 4) / 0              # => ZeroDivisionError
[EXCEPTION] ZeroDivisionError:
other が 0 の時に発生します。
self <=> other -> -1 | 0 | 1 | nil

self と other を比較して、self が大きい時に 1、等しい時に 0、小さい時に -1 を返します。比較できない場合はnilを返します。

[PARAM] other:
自身と比較する数値
[RETURN]
-1 か 0 か 1 か nil を返します。

例:

Rational(2, 3)  <=> Rational(2, 3)  # => 0
Rational(5)     <=> 5               # => 0
Rational(2, 3)  <=> Rational(1,3)   # => 1
Rational(1, 3)  <=> 1               # => -1
Rational(1, 3)  <=> 0.3             # => 1
Rational(1, 3)  <=> nil             # => nil
self == other -> bool

数値として等しいか判定します。

[PARAM] other:
自身と比較する数値
[RETURN]
self と other が等しい場合 true を返します。 そうでなければ false を返します。

例:

Rational(2, 3)  == Rational(2, 3)   # => true
Rational(5)     == 5                # => true
Rational(0)     == 0.0              # => true
Rational(1, 3)  == 0.33             # => false
Rational(1, 2)  == '1/2'            # => false

注意:

Rational.new! で作成したオブジェクトと比較した場合、同じ数値を表 すオブジェクトでも true を返さない事があります。

Rational(1,2) == Rational(4,8)          # => true
Rational(1,2) == Rational.new!(4,8)     # => false

詳しくは Rational.new! を確認してください。

abs -> Rational

自身の絶対値を返します。

例:

Rational(1, 2).abs.to_s  # => 1/2
Rational(-1, 2).abs.to_s # => 1/2
ceil -> Integer

自身と等しいかより大きな整数のうち最小のものを返します。

例:

Rational(3).ceil      # => 3
Rational(2, 3).ceil   # => 1
Rational(-3, 2).ceil  # => -1

[SEE_ALSO] Rational#floor, Rational#round, Rational#truncate

coerce(other) -> Array

自身と other が同じクラスになるよう、自身か other を変換し [other, self] という 配列にして返します。

[PARAM] other:
比較または変換するオブジェクト

例:

Rational(1).coerce(2)   # => [Rational(2, 1), Rational(1, 1)]
Rational(1).coerce(2.2) # => [2.2, 1.0]
denominator -> Integer

分母を返します。常に正の整数を返します。

[RETURN]
分母を返します。

例:

Rational(7).denominator       # => 1
Rational(7, 1).denominator    # => 1
Rational(9, -4).denominator   # => 4
Rational(-2, -10).denominator # => 5

[SEE_ALSO] Rational#numerator

div(other) -> Integer

self を other で割った整数の商を返します。

[PARAM] other:
自身を割る数

例:

Rational(1, 2).div(Rational(2, 3)) # => 0
divmod(other) -> [Integer, Float | Rational]

self を other で割った、商と余りの配列を返します。

other に Float を指定した場合は、余りを Float で返します。

[PARAM] other:
自身を割る数

例:

Rational(3,4).divmod(Rational(2,3))  # => [1, Rational(1, 12)]
Rational(-3,4).divmod(Rational(2,3)) # => [-2, Rational(7, 12)]
Rational(3,4).divmod(Rational(-2,3)) # => [-2, Rational(-7, 12)]

Rational(9,4).divmod(2)              # => [1, Rational(1, 4)]
Rational(9,4).divmod(Rational(2, 1)) # => [1, Rational(1, 4)]
Rational(9,4).divmod(2.0)            # => [1, 0.25]

[SEE_ALSO] Numeric#divmod

floor -> Integer

自身と等しいかより小さな整数のうち最大のものを返します。

例:

Rational(3).floor     # => 3
Rational(2, 3).floor  # => 0
Rational(-3, 2).floor # => -2

自身にもっとも近い整数を返す Rational#to_i とは違う結果を返す事に 注意してください。

例:

Rational(+7, 4).to_i  # => 1
Rational(+7, 4).floor # => 1
Rational(-7, 4).to_i  # => -1
Rational(-7, 4).floor # => -2

[SEE_ALSO] Rational#ceil, Rational#round, Rational#truncate

hash -> Integer

自身のハッシュ値を返します。

[RETURN]
ハッシュ値を返します。
inspect [redefined by mathn]

有理数値を人間が読みやすい形の文字列表現にして返します。

現在のバージョンでは "3/5", "-17/7" のように10進数の既約分数表記を返します。

inspect -> String

自身を"Rational(分子, 分母)" 形式の文字列にして返します。

[RETURN]
文字列を返します。

例:

Rational(5, 8).inspect  # => "Rational(5, 8)"
Rational(2).inspect     # => "Rational(2, 1)"
Rational(-8, 6).inspect # => "Rational(-4, 3)"

1.9系とは結果が異なる事に注意してください。

# 1.9.1の場合
Rational(5, 8).inspect  # => "(5/8)"
Rational(2).inspect     # => "(2/1)"
Rational(-8, 6).inspect # => "(-4/3)"

[SEE_ALSO] Rational#to_s

numerator -> Integer

分子を返します。

[RETURN]
分子を返します。

例:

Rational(7).numerator       # => 7
Rational(7, 1).numerator    # => 7
Rational(9, -4).numerator   # => -9
Rational(-2, -10).numerator # => 1

[SEE_ALSO] Rational#denominator

power2 [redefined by mathn]

[TODO]

round -> Integer

自身ともっとも近い整数を返します。

中央値 0.5, -0.5 はそれぞれ 1,-1 に切り上げされます。

例:

Rational(3).round     # => 3
Rational(2, 3).round  # => 1
Rational(-3, 2).round # => -2

[SEE_ALSO] Rational#ceil, Rational#floor, Rational#truncate

to_f -> Float

自身を Float に変換します。

[RETURN]
実数を返します。

例:

Rational(9, 4).to_f   # => 2.25
Rational(-3, 4).to_f  # => -0.75
Rational(20, 3).to_f  # => 6.666666666666667
truncate -> Integer
to_i -> Integer

0 から 自身までの整数で、自身にもっとも近い整数を返します。

例:

Rational(2, 3).to_i   # => 0
Rational(3).to_i      # => 3
Rational(98, 71).to_i # => 1
Rational(-30, 2).to_i # => -15

[SEE_ALSO] Rational#ceil, Rational#floor

to_r -> Rational

自身を返します。

[RETURN]
自身を返します。
to_s -> String

自身を人間が読みやすい形の文字列表現にして返します。

"3/5", "-17/7" のように10進数の表記を返します。

[RETURN]
有理数の表記にした文字列を返します。

例:

Rational(-3, 4).to_s # => "-3/4"
Rational(8).to_s     # => "8"
Rational(-8, 6).to_s # => "-4/3"

[SEE_ALSO] Rational#inspect

class Rational