class Rational
A rational number can be represented as a pair of integer numbers: a/b (b>0), where a is the numerator and b is the denominator. Integer a equals rational a/1 mathematically.
You can create a Rational object explicitly with:
-
A rational literal.
You can convert certain objects to Rationals with:
-
Method#Rational.
Examples
Rational(1) #=> (1/1) Rational(2, 3) #=> (2/3) Rational(4, -6) #=> (-2/3) # Reduced. 3.to_r #=> (3/1) 2/3r #=> (2/3)
You can also create rational objects 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 programs 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 includes an inexact component (numerical value or operation), it 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)
Public Class Methods
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 49
def %: (Integer) -> Rational
| (Float) -> Float
| (Rational) -> Rational
| (Numeric) -> Numeric
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1407
def *: (BigDecimal) -> BigDecimal
| ...
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
Returns the numeric product of self and other:
Rational(9, 8) * 4 #=> (9/2) Rational(20, 9) * 9.8 #=> 21.77777777777778 Rational(9, 8) * Complex(1, 2) # => ((9/8)+(9/4)*i) Rational(2, 3) * Rational(2, 3) #=> (4/9) Rational(900) * Rational(1) #=> (900/1) Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
Returns the numeric product of self and other:
Rational(9, 8) * 4 #=> (9/2) Rational(20, 9) * 9.8 #=> 21.77777777777778 Rational(9, 8) * Complex(1, 2) # => ((9/8)+(9/4)*i) Rational(2, 3) * Rational(2, 3) #=> (4/9) Rational(900) * Rational(1) #=> (900/1) Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 84
def **: (Complex) -> Complex
| (Numeric) -> Numeric
Returns self raised to the power exponent:
Rational(2) ** Rational(3) #=> (8/1) Rational(10) ** -2 #=> (1/100) Rational(10) ** -2.0 #=> 0.01 Rational(-4) ** Rational(1, 2) #=> (0.0+2.0i) Rational(1, 2) ** 0 #=> (1/1) Rational(1, 2) ** 0.0 #=> 1.0
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1422
def +: (BigDecimal) -> BigDecimal
| ...
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
Returns the sum of self and other:
Rational(2, 3) + 0 # => (2/3) Rational(2, 3) + 1 # => (5/3) Rational(2, 3) + -1 # => (-1/3) Rational(2, 3) + Complex(1, 0) # => ((5/3)+0i) Rational(2, 3) + Rational(1, 1) # => (5/3) Rational(2, 3) + Rational(3, 2) # => (13/6) Rational(2, 3) + Rational(3.0, 2.0) # => (13/6) Rational(2, 3) + Rational(3.1, 2.1) # => (30399297484750849/14186338826217063)
For a computation involving Floats, the result may be inexact (see Float#+):
Rational(2, 3) + 1.0 # => 1.6666666666666665 Rational(2, 3) + Complex(1.0, 0.0) # => (1.6666666666666665+0.0i)
Returns the sum of self and other:
Rational(2, 3) + 0 # => (2/3) Rational(2, 3) + 1 # => (5/3) Rational(2, 3) + -1 # => (-1/3) Rational(2, 3) + Complex(1, 0) # => ((5/3)+0i) Rational(2, 3) + Rational(1, 1) # => (5/3) Rational(2, 3) + Rational(3, 2) # => (13/6) Rational(2, 3) + Rational(3.0, 2.0) # => (13/6) Rational(2, 3) + Rational(3.1, 2.1) # => (30399297484750849/14186338826217063)
For a computation involving Floats, the result may be inexact (see Float#+):
Rational(2, 3) + 1.0 # => 1.6666666666666665 Rational(2, 3) + Complex(1.0, 0.0) # => (1.6666666666666665+0.0i)
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1437
def -: (BigDecimal) -> BigDecimal
| ...
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
Returns the difference of self and other:
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
Returns the difference of self and other:
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 138
def -@: () -> Rational
Returns self, negated:
-(1/3r) # => (-1/3) -(-1/3r) # => (1/3)
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1392
def /: (BigDecimal) -> BigDecimal
| ...
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
Returns the quotient of self and other:
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
Returns the quotient of self and other:
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 182
def <=>: (Integer | Rational) -> Integer
| (untyped) -> Integer?
Compares self and other.
Returns:
-
-1, ifselfis less thanother. -
0, if the two values are the same. -
1, ifselfis greater thanother. -
nil, if the two values are incomparable.
Examples:
Rational(2, 3) <=> Rational(4, 3) # => -1 Rational(2, 1) <=> Rational(2, 1) # => 0 Rational(2, 1) <=> 2 # => 0 Rational(2, 1) <=> 2.0 # => 0 Rational(2, 1) <=> Complex(2, 0) # => 0 Rational(4, 3) <=> Rational(2, 3) # => 1 Rational(4, 3) <=> :foo # => nil
Class Rational includes module Comparable, each of whose methods uses Rational#<=> for comparison.
(untyped) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 197
def ==: (untyped) -> bool
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 209
def abs: () -> Rational
Returns the absolute value of rat.
(1/2r).abs #=> (1/2) (-1/2r).abs #=> (1/2)
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/rational.rb, line 29 def as_json(*) { JSON.create_id => self.class.name, 'n' => numerator, 'd' => denominator, } end
Methods Rational#as_json and Rational.json_create may be used to serialize and deserialize a Rational object; see Marshal.
Method Rational#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/rational' x = Rational(2, 3).as_json # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
Method JSON.create deserializes such a hash, returning a Rational object:
Rational.json_create(x) # => (2/3)
Methods Rational#as_json and Rational.json_create may be used to serialize and deserialize a Rational object; see Marshal.
Method Rational#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/rational' x = Rational(2, 3).as_json # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
Method JSON.create deserializes such a hash, returning a Rational object:
Rational.json_create(x) # => (2/3)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 234
def ceil: () -> Integer
| (Integer digits) -> (Integer | Rational)
Returns the smallest number greater than or equal to rat with a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns a rational when ndigits is positive, otherwise returns an integer.
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 Rational('-123.456').ceil(+1).to_f #=> -123.4 Rational('-123.456').ceil(-1) #=> -120
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 237
def coerce: (Numeric) -> [ Numeric, Numeric ]
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 250
def 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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 252
def divmod: (Integer | Float | Rational) -> [ Integer, Rational ]
| (Numeric) -> [ Numeric, Numeric ]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 265
def 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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 290
def floor: () -> Integer
| (Integer digits) -> (Integer | Rational)
Returns the largest number less than or equal to rat with a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns a rational when ndigits is positive, otherwise returns an integer.
Rational(3).floor #=> 3 Rational(2, 3).floor #=> 0 Rational(-3, 2).floor #=> -2 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').floor(+1).to_f #=> -123.5 Rational('-123.456').floor(-1) #=> -130
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 298
def hash: () -> Integer
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 310
def 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)"
Returns the absolute value of rat.
(1/2r).abs #=> (1/2) (-1/2r).abs #=> (1/2)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 320
def modulo: (Float) -> Float
| (Numeric) -> Rational
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 329
def negative?: () -> bool
Returns true if rat is less than 0.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 342
def numerator: () -> Integer
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 344
def polar: () -> [ Rational, Integer | Float ]
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 352
def positive?: () -> bool
Returns true if rat is greater than 0.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 363
def quo: (Float) -> Float
| (Complex) -> Complex
| (Numeric) -> Rational
Returns the quotient of self and other:
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 380
def rationalize: (?Numeric 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)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 382
def rect: () -> [ Rational, Numeric ]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 384
def remainder: (Float) -> Float
| (Numeric) -> Rational
(?half: :up | :down | :even) → Integer
(Integer digits, ?half: :up | :down | :even) → (Integer | Rational)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 422
def round: (?half: :up | :down | :even) -> Integer
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)
Returns rat rounded to the nearest value with a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns a rational when ndigits is positive, otherwise returns an integer.
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 Rational('-123.456').round(+1).to_f #=> -123.5 Rational('-123.456').round(-1) #=> -120
The optional half keyword argument is available similar to Float#round.
Rational(25, 100).round(1, half: :up) #=> (3/10) Rational(25, 100).round(1, half: :down) #=> (1/5) Rational(25, 100).round(1, half: :even) #=> (1/5) Rational(35, 100).round(1, half: :up) #=> (2/5) Rational(35, 100).round(1, half: :down) #=> (3/10) Rational(35, 100).round(1, half: :even) #=> (2/5) Rational(-25, 100).round(1, half: :up) #=> (-3/10) Rational(-25, 100).round(1, half: :down) #=> (-1/5) Rational(-25, 100).round(1, half: :even) #=> (-1/5)
(Integer precision) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/lib/bigdecimal/util.rb, line 135 def to_d(precision=0) BigDecimal(self, precision) end
Returns the value as a BigDecimal.
The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' Rational(22, 7).to_d(3) # => 0.314e1
See also Kernel.BigDecimal.
Returns the value as a BigDecimal.
The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' Rational(22, 7).to_d(3) # => 0.314e1
See also Kernel.BigDecimal.
Returns the value as a BigDecimal.
The required precision parameter is used to determine the number of significant digits for the result.
require 'bigdecimal' require 'bigdecimal/util' Rational(22, 7).to_d(3) # => 0.314e1
See also Kernel.BigDecimal.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 436
def to_f: () -> Float
Returns 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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 452
def to_i: () -> Integer
Returns the truncated value as an integer.
Equivalent to Rational#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(-31, 2).to_i #=> -15
(?JSON::State? state) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/rational.rb, line 46 def to_json(*args) as_json.to_json(*args) end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 463
def to_r: () -> Rational
Returns self.
Rational(2).to_r #=> (2/1) Rational(-8, 6).to_r #=> (-4/3)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 475
def 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"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/core/rational.rbs, line 500
def truncate: () -> Integer
| (Integer ndigits) -> (Integer | Rational)
Returns rat truncated (toward zero) to a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns a rational when ndigits is positive, otherwise returns an integer.
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 Rational('-123.456').truncate(+1).to_f #=> -123.4 Rational('-123.456').truncate(-1) #=> -120