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.



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

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

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



4
5
6
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 4

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



4
5
6
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 4

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



4
5
6
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 4

def c
  @c
end

Instance Method Details

#deltaObject



18
19
20
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 18

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

#root(signal) ⇒ Object



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

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

#rootsObject



12
13
14
15
16
# File 'lib/ehb_game_lib/math/quadratic_equation.rb', line 12

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