Class: Maxima::Rational
Constant Summary collapse
- REGEX =
/\s*(\d+)\s*\/\s*(\d+)\s*/
Instance Attribute Summary collapse
-
#denominator ⇒ Object
Returns the value of attribute denominator.
-
#numerator ⇒ Object
Returns the value of attribute numerator.
Attributes inherited from Unit
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #imaginary? ⇒ Boolean
-
#initialize(numerator, denominator, **options) ⇒ Rational
constructor
A new instance of Rational.
- #real? ⇒ Boolean
- #to_f ⇒ Object
Methods inherited from Unit
#==, #===, #at, #gnu_plot_options, #gnu_plot_text, #gnu_plot_w, #inspect, #negative?, parse_float, #positive?, #simplified, #through_maxima, #to_gnu_plot, #to_maxima_input, #to_pdf, #to_s, #with_plot_title, #zero?
Constructor Details
#initialize(numerator, denominator, **options) ⇒ Rational
Returns a new instance of Rational.
6 7 8 9 10 |
# File 'lib/maxima/rational.rb', line 6 def initialize(numerator, denominator, **) super(**) @numerator = numerator @denominator = denominator end |
Instance Attribute Details
#denominator ⇒ Object
Returns the value of attribute denominator.
4 5 6 |
# File 'lib/maxima/rational.rb', line 4 def denominator @denominator end |
#numerator ⇒ Object
Returns the value of attribute numerator.
4 5 6 |
# File 'lib/maxima/rational.rb', line 4 def numerator @numerator end |
Class Method Details
.parse(maxima_output) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/maxima/rational.rb', line 13 def self.parse(maxima_output) _, numerator, denominator = REGEX.match(maxima_output).to_a return if numerator.nil? || denominator.nil? if numerator == 0 Float.new(0, maxima_output: maxima_output) else Rational.new(numerator.to_f, denominator.to_f, maxima_output: maxima_output) end rescue StandardError nil end |
Instance Method Details
#<=>(other) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/maxima/rational.rb', line 39 def <=>(other) case other when ::Float, ::Rational, Float, Rational self.to_f <=> other.to_f else -1 end end |
#to_f ⇒ Object
35 36 37 |
# File 'lib/maxima/rational.rb', line 35 def to_f @to_f ||= numerator.fdiv(denominator) end |