class Quadratic::Real

Abstract class for Quadratic[d] (d > 0).

Public Instance Methods

<=>(other) click to toggle source

@param [Object] other @return [+1/0/-1/nil]

# File lib/quadratic_number/real.rb, line 31
def <=>(other)
        if other.kind_of?(Numeric)
                if other.real?
                        (self - other).to_f <=> 0
                else
                        n1, n2 = other.coerce(self)
                        n1 <=> n2
                end
        else
                nil
        end
end
==(other) click to toggle source

@param [Object] other @return [Boolean]

# File lib/quadratic_number/real.rb, line 48
def ==(other)
        if other.kind_of?(Numeric) && other.real?
                (self - other).to_f == 0
        else
                other == self
        end
end
to_f() click to toggle source

Returns a float. Prevents cancellations of significant digits.

@return [Float]

@example

-665857 + 470832 * Math.sqrt(2)        #=> -7.508788257837296e-07
Quadratic[2].new(-665857, 470832).to_f #=> -7.509119826032946e-07
# File lib/quadratic_number/real.rb, line 71
def to_f
        d = self.class::D
        if @a * @b < 0
                # Using #fdiv instead of #/
                # because Rational#/ returns Rational for a big Float divisor in ruby2.0
                (@a * @a - @b * @b * d).fdiv(@a - @b * Math.sqrt(d))
        else
                @a + @b * Math.sqrt(d)
        end
end