Class: Rational
- Inherits:
-
Numeric
- Object
- Numeric
- Rational
- Defined in:
- lib/bigdecimal/util.rb,
sig/big_decimal.rbs,
sig/big_decimal_util.rbs
Instance Method Summary collapse
-
#* ⇒ Object
Performs multiplication.
-
#+ ⇒ Object
Performs addition.
-
#- ⇒ Object
Performs subtraction.
-
#/ ⇒ Object
Performs division.
-
#to_d(precision = 0) ⇒ BigDecimal
Returns the value as a BigDecimal.
Instance Method Details
#*(arg0) ⇒ BigDecimal #* ⇒ void
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
1409 1410 |
# File 'sig/big_decimal.rbs', line 1409 def *: (BigDecimal) -> BigDecimal | ... |
#+(arg0) ⇒ BigDecimal #+ ⇒ void
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
1424 1425 |
# File 'sig/big_decimal.rbs', line 1424 def +: (BigDecimal) -> BigDecimal | ... |
#-(arg0) ⇒ BigDecimal #- ⇒ void
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
1439 1440 |
# File 'sig/big_decimal.rbs', line 1439 def -: (BigDecimal) -> BigDecimal | ... |
#/(arg0) ⇒ BigDecimal #/ ⇒ void
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
1394 1395 |
# File 'sig/big_decimal.rbs', line 1394 def /: (BigDecimal) -> BigDecimal | ... |
#to_d(precision = 0) ⇒ 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.
135 136 137 |
# File 'lib/bigdecimal/util.rb', line 135 def to_d(precision=0) BigDecimal(self, precision) end |