Class: EhbGameLib::Math::QuadraticEquation

Inherits:
Object
  • Object
show all
Defined in:
lib/ehb_game_lib/math/quadratic_equation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b, c) ⇒ QuadraticEquation

Returns a new instance of QuadraticEquation.



8
9
10
11
12
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 8

def initialize(a, b, c)
  @a = a
  @b = b
  @c = c
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



6
7
8
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 6

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



6
7
8
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 6

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



6
7
8
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 6

def c
  @c
end

Instance Method Details

#deltaObject



21
22
23
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 21

def delta
  @delta ||= b**2 - 4 * a * c
end

#root(signal) ⇒ Object



25
26
27
28
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 25

def root(signal)
  signal = signal.negative? ? -1 : 1
  (-b + signal * ::Math.sqrt(delta)) / (2 * a)
end

#rootsObject



14
15
16
17
18
19
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 14

def roots
  return [root(1), root(-1)] if delta > 0.0
  return [root(1)] if delta == 0.0

  []
end