Module: VectorNumber::Converting
- Included in:
- VectorNumber
- Defined in:
- lib/vector_number/converting.rb
Overview
Methods for converting to different number classes.
Instance Method Summary collapse
-
#imaginary ⇒ Integer, ...
(also: #imag)
Return imaginary part of the number.
-
#real ⇒ Integer, ...
Return real part of the number.
-
#to_c ⇒ Complex
Return value as a Complex if only real and/or imaginary parts are non-zero.
-
#to_d(ndigits = nil) ⇒ BigDecimal
Return value as a BigDecimal if only real part is non-zero.
-
#to_f ⇒ Float
Return value as a Float if only real part is non-zero.
-
#to_i ⇒ Integer
(also: #to_int)
Return value as an Integer, truncating it, if only real part is non-zero.
-
#to_r ⇒ Rational
Return value as a Rational if only real part is non-zero.
Instance Method Details
#imaginary ⇒ Integer, ... Also known as: imag
Return imaginary part of the number.
26 |
# File 'lib/vector_number/converting.rb', line 26 def imaginary = @data[I] |
#real ⇒ Integer, ...
Return real part of the number.
15 |
# File 'lib/vector_number/converting.rb', line 15 def real = @data[R] |
#to_c ⇒ Complex
Return value as a Complex if only real and/or imaginary parts are non-zero.
142 143 144 |
# File 'lib/vector_number/converting.rb', line 142 def to_c numeric?(2) ? Complex(real, imaginary) : raise_convert_error(Complex) end |
#to_d(ndigits = nil) ⇒ BigDecimal
Return value as a BigDecimal if only real part is non-zero.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/vector_number/converting.rb', line 116 def to_d(ndigits = nil) if numeric?(1) return BigDecimal(real, ndigits) if ndigits return BigDecimal(real, Float::DIG) if real.is_a?(Float) BigDecimal(real) else raise_convert_error(BigDecimal) end end |
#to_f ⇒ Float
Return value as a Float if only real part is non-zero.
69 70 71 |
# File 'lib/vector_number/converting.rb', line 69 def to_f numeric?(1) ? real.to_f : raise_convert_error(Float) end |
#to_i ⇒ Integer Also known as: to_int
Return value as an Integer, truncating it, if only real part is non-zero.
47 48 49 |
# File 'lib/vector_number/converting.rb', line 47 def to_i numeric?(1) ? real.to_i : raise_convert_error(Integer) end |
#to_r ⇒ Rational
Return value as a Rational if only real part is non-zero.
88 89 90 |
# File 'lib/vector_number/converting.rb', line 88 def to_r numeric?(1) ? real.to_r : raise_convert_error(Rational) end |