Class: Rational
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Numeric
|
|
Instance Chain:
self,
::Numeric,
::Comparable
|
|
Inherits: | Numeric |
Defined in: | rational.c |
Overview
A rational number can be represented as a paired integer number; a/b (b>0). Where a is numerator and b is denominator. ::Integer a equals rational a/1 mathematically.
In ruby, you can create rational object with Rational
, to_r, rationalize method or suffixing r to a literal. The return values will be irreducible.
Rational(1) #=> (1/1)
Rational(2, 3) #=> (2/3)
Rational(4, -6) #=> (-2/3)
3.to_r #=> (3/1)
2/3r #=> (2/3)
You can also create rational object from floating-point numbers or strings.
Rational(0.3) #=> (5404319552844595/18014398509481984)
Rational('0.3') #=> (3/10)
Rational('2/3') #=> (2/3)
0.3.to_r #=> (5404319552844595/18014398509481984)
'0.3'.to_r #=> (3/10)
'2/3'.to_r #=> (2/3)
0.3.rationalize #=> (3/10)
A rational object is an exact number, which helps you to write program without any rounding errors.
10.times.inject(0){|t,| t + 0.1} #=> 0.9999999999999999
10.times.inject(0){|t,| t + Rational('0.1')} #=> (1/1)
However, when an expression has inexact factor (numerical value or operation), will produce an inexact result.
Rational(10) / 3 #=> (10/3)
Rational(10) / 3.0 #=> 3.3333333333333335
Rational(-8) ** Rational(1, 3)
#=> (1.0000000000000002+1.7320508075688772i)
Instance Attribute Summary
- #exact? (also: #rational?) readonly
-
#negative? ⇒ Boolean
readonly
Returns
true
ifrat
is less than 0. -
#positive? ⇒ Boolean
readonly
Returns
true
ifrat
is greater than 0.
::Numeric - Inherited
#finite? | Return true if |
#infinite? | Returns values corresponding to the value of |
#integer? | Returns |
#negative? | Returns |
#nonzero? | Returns |
#positive? | Returns |
#real | Returns self. |
#real? | Returns |
#zero? | Returns |
Instance Method Summary
-
#*(numeric) ⇒ Numeric
Performs multiplication.
-
#**(numeric) ⇒ Numeric
Performs exponentiation.
-
#+(numeric) ⇒ Numeric
Performs addition.
-
#-(numeric) ⇒ Numeric
Performs subtraction.
-
#- ⇒ Rational
Negates
rat
. -
#/(numeric) ⇒ Numeric
(also: #quo)
Performs division.
-
#<=>(numeric) ⇒ 1, ...
Performs comparison and returns -1, 0, or +1.
-
#==(object) ⇒ Boolean
Returns true if rat equals object numerically.
-
#abs ⇒ rat
(also: #magnitude)
Returns the absolute value of
rat
. -
#ceil ⇒ Integer
Returns the truncated value (toward positive infinity).
-
#denominator ⇒ Integer
Returns the denominator (always positive).
-
#fdiv(numeric) ⇒ Float
Performs division and returns the value as a float.
-
#floor ⇒ Integer
Returns the truncated value (toward negative infinity).
-
#inspect ⇒ String
Returns the value as a string for inspection.
-
#magnitude ⇒ rat
Alias for #abs.
-
#numerator ⇒ Integer
Returns the numerator.
-
#quo(numeric) ⇒ Numeric
Alias for #/.
-
#rationalize ⇒ self
Returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
-
#round ⇒ Integer
Returns the truncated value (toward the nearest integer; 0.5 => 1; -0.5 => -1).
-
#to_f ⇒ Float
Return the value as a float.
-
#to_i ⇒ Integer
Returns the truncated value as an integer.
-
#to_r ⇒ self
Returns self.
-
#to_s ⇒ String
Returns the value as a string.
-
#truncate ⇒ Integer
Returns the truncated value (toward zero).
::Numeric - Inherited
#% | x.modulo(y) means x-y*(x/y).floor. |
#+@ | Unary Plus—Returns the receiver's value. |
#-@ | Unary Minus—Returns the receiver's value, negated. |
#<=> | Returns zero if |
#abs | Returns the absolute value of |
#abs2 | Returns square of self. |
#angle | Alias for Numeric#arg. |
#arg | Returns 0 if the value is positive, pi otherwise. |
#ceil | Returns the smallest possible ::Integer that is greater than or equal to |
#coerce, | |
#conj | Returns self. |
#conjugate | Alias for Numeric#conj. |
#denominator | Returns the denominator (always positive). |
#div | Uses #/ to perform division, then converts the result to an integer. |
#divmod | Returns an array containing the quotient and modulus obtained by dividing |
#eql? | Returns |
#fdiv | Returns float division. |
#floor | Returns the largest integer less than or equal to |
#i | Returns the corresponding imaginary number. |
#imag | Returns zero. |
#imaginary | Alias for Numeric#imag. |
#initialize_copy | Numerics are immutable values, which should not be copied. |
#magnitude | Alias for Numeric#abs. |
#modulo | Alias for Numeric#%. |
#numerator | Returns the numerator. |
#phase | Alias for Numeric#arg. |
#polar | Returns an array; [num.abs, num.arg]. |
#quo | Returns most exact division (rational for integers, float for floats). |
#rect | Returns an array; [num, 0]. |
#rectangular | Alias for Numeric#rect. |
#remainder | x.remainder(y) means x-y*(x/y).truncate. |
#round | Rounds |
#singleton_method_added | Trap attempts to add methods to ::Numeric objects. |
#step | Invokes the given block with the sequence of numbers starting at |
#to_c | Returns the value as a complex. |
#to_int | Invokes the child class's #to_i method to convert |
#truncate | Returns |
::Comparable - Included
#< | Compares two objects based on the receiver's #<=> method, returning true if it returns -1. |
#<= | Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0. |
#== | Compares two objects based on the receiver's #<=> method, returning true if it returns 0. |
#> | Compares two objects based on the receiver's #<=> method, returning true if it returns 1. |
#>= | Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1. |
#between? | |
#clamp |
Instance Attribute Details
#exact? (readonly) Also known as: #rational?
#negative? ⇒ Boolean
(readonly)
Returns true
if rat
is less than 0.
#positive? ⇒ Boolean
(readonly)
Returns true
if rat
is greater than 0.
Instance Method Details
#*(numeric) ⇒ Numeric
Performs multiplication.
Rational(2, 3) * Rational(2, 3) #=> (4/9)
Rational(900) * Rational(1) #=> (900/1)
Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
Rational(9, 8) * 4 #=> (9/2)
Rational(20, 9) * 9.8 #=> 21.77777777777778
#**(numeric) ⇒ Numeric
Performs exponentiation.
Rational(2) ** Rational(3) #=> (8/1)
Rational(10) ** -2 #=> (1/100)
Rational(10) ** -2.0 #=> 0.01
Rational(-4) ** Rational(1,2) #=> (1.2246063538223773e-16+2.0i)
Rational(1, 2) ** 0 #=> (1/1)
Rational(1, 2) ** 0.0 #=> 1.0
#+(numeric) ⇒ Numeric
Performs addition.
Rational(2, 3) + Rational(2, 3) #=> (4/3)
Rational(900) + Rational(1) #=> (901/1)
Rational(-2, 9) + Rational(-9, 2) #=> (-85/18)
Rational(9, 8) + 4 #=> (41/8)
Rational(20, 9) + 9.8 #=> 12.022222222222222
#-(numeric) ⇒ Numeric
Performs subtraction.
Rational(2, 3) - Rational(2, 3) #=> (0/1)
Rational(900) - Rational(1) #=> (899/1)
Rational(-2, 9) - Rational(-9, 2) #=> (77/18)
Rational(9, 8) - 4 #=> (23/8)
Rational(20, 9) - 9.8 #=> -7.577777777777778
#- ⇒ Rational
Negates rat
.
Also known as: #quo
Performs division.
Rational(2, 3) / Rational(2, 3) #=> (1/1)
Rational(900) / Rational(1) #=> (900/1)
Rational(-2, 9) / Rational(-9, 2) #=> (4/81)
Rational(9, 8) / 4 #=> (9/32)
Rational(20, 9) / 9.8 #=> 0.22675736961451246
#<=>(numeric) ⇒ 1
, ...
Performs comparison and returns -1, 0, or +1.
nil
is returned if the two values are incomparable.
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
#==(object) ⇒ Boolean
Returns true if rat equals object numerically.
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
#abs ⇒ rat
#magnitude ⇒ rat
Also known as: #magnitude
rat
#magnitude ⇒ rat
Returns the absolute value of rat
.
(1/2r).abs #=> 1/2r
(-1/2r).abs #=> 1/2r
#magnitude is an alias of abs
.
#ceil ⇒ Integer
#ceil(precision = 0) ⇒ Rational
Rational
Returns the truncated value (toward positive infinity).
Rational(3).ceil #=> 3
Rational(2, 3).ceil #=> 1
Rational(-3, 2).ceil #=> -1
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
'%f' % Rational('-123.456').ceil(+1) #=> "-123.400000"
'%f' % Rational('-123.456').ceil(-1) #=> "-120.000000"
#denominator ⇒ Integer
Returns the denominator (always positive).
Rational(7).denominator #=> 1
Rational(7, 1).denominator #=> 1
Rational(9, -4).denominator #=> 4
Rational(-2, -10).denominator #=> 5
rat.numerator.gcd(rat.denominator) #=> 1
#fdiv(numeric) ⇒ Float
Performs division and returns the value as a float.
Rational(2, 3).fdiv(1) #=> 0.6666666666666666
Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333
Rational(2).fdiv(3) #=> 0.6666666666666666
#floor ⇒ Integer
#floor(precision = 0) ⇒ Rational
Rational
Returns the truncated value (toward negative infinity).
Rational(3).floor #=> 3
Rational(2, 3).floor #=> 0
Rational(-3, 2).floor #=> -1
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
'%f' % Rational('-123.456').floor(+1) #=> "-123.500000"
'%f' % Rational('-123.456').floor(-1) #=> "-130.000000"
#inspect ⇒ String
Returns the value as a string for inspection.
Rational(2).inspect #=> "(2/1)"
Rational(-8, 6).inspect #=> "(-4/3)"
Rational('1/2').inspect #=> "(1/2)"
#abs ⇒ rat
#magnitude ⇒ rat
rat
#magnitude ⇒ rat
Alias for #abs.
#numerator ⇒ Integer
Returns the numerator.
Rational(7).numerator #=> 7
Rational(7, 1).numerator #=> 7
Rational(9, -4).numerator #=> -9
Rational(-2, -10).numerator #=> 1
Alias for #/.
#rationalize ⇒ self
#rationalize(eps) ⇒ Rational
self
#rationalize(eps) ⇒ Rational
Returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216)
r.rationalize #=> (5033165/16777216)
r.rationalize(Rational('0.01')) #=> (3/10)
r.rationalize(Rational('0.1')) #=> (1/3)
#round ⇒ Integer
#round(precision = 0) ⇒ Rational
Rational
Returns the truncated value (toward the nearest integer; 0.5 => 1; -0.5 => -1).
Rational(3).round #=> 3
Rational(2, 3).round #=> 1
Rational(-3, 2).round #=> -2
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
'%f' % Rational('-123.456').round(+1) #=> "-123.500000"
'%f' % Rational('-123.456').round(-1) #=> "-120.000000"
#to_f ⇒ Float
Return the value as a float.
Rational(2).to_f #=> 2.0
Rational(9, 4).to_f #=> 2.25
Rational(-3, 4).to_f #=> -0.75
Rational(20, 3).to_f #=> 6.666666666666667
#to_i ⇒ Integer
Returns the truncated value as an integer.
Equivalent to #truncate.
Rational(2, 3).to_i #=> 0
Rational(3).to_i #=> 3
Rational(300.6).to_i #=> 300
Rational(98,71).to_i #=> 1
Rational(-30,2).to_i #=> -15
#to_r ⇒ self
Returns self.
Rational(2).to_r #=> (2/1)
Rational(-8, 6).to_r #=> (-4/3)
#to_s ⇒ String
Returns the value as a string.
Rational(2).to_s #=> "2/1"
Rational(-8, 6).to_s #=> "-4/3"
Rational('1/2').to_s #=> "1/2"
#truncate ⇒ Integer
#truncate(precision = 0) ⇒ Rational
Rational
Returns the truncated value (toward zero).
Rational(3).truncate #=> 3
Rational(2, 3).truncate #=> 0
Rational(-3, 2).truncate #=> -1
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
'%f' % Rational('-123.456').truncate(+1) #=> "-123.400000"
'%f' % Rational('-123.456').truncate(-1) #=> "-120.000000"