Class: Quadratic::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/quadratic_number/real.rb

Overview

< Quadratic < Numeric

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

Comparisons ###



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quadratic_number/real.rb', line 24

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) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/quadratic_number/real.rb', line 37

def ==(other)
	if other.kind_of?(Numeric) && other.real?
		(self - other).to_f == 0
	else
		other == self
	end
end

#to_fObject

defined by Numeric:

  • to_c #=> Complex.rect(self, 0)



50
51
52
53
54
55
56
57
58
59
# File 'lib/quadratic_number/real.rb', line 50

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