Class: Sqrt::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/sqrt/object.rb

Instance Method Summary collapse

Constructor Details

#initialize(coefficient, x) ⇒ Object

x type expects only Integer, Float, Rela in v0.1.0



3
4
5
6
# File 'lib/sqrt/object.rb', line 3

def initialize(coefficient, x)
  @coefficient = coefficient
  @value = x
end

Instance Method Details

#*(target) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/sqrt/object.rb', line 12

def *(target)
  if target.class == Sqrt::Object
    Sqrt::Object.new(@coefficient * target.instance_variable_get(:@coefficient), @value * target.instance_variable_get(:@value))
  elsif target.class <= Numeric
    Sqrt::Object.new(@coefficient * target, @value)
  end
end

#**(x) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/sqrt/object.rb', line 20

def **(x)
  return 1 if x.zero?

  if x.class == Integer
    x.even? ? (@coefficient ** x) * (@value ** (x / 2)) : Sqrt::Object.new((@coefficient ** x) * (@value ** (x / 2)), @value)
  else
    self.to_f ** x
  end
end

#to_fObject



8
9
10
# File 'lib/sqrt/object.rb', line 8

def to_f
  @coefficient * (@value.positive? ? Math.sqrt(@value) : Complex(0, Math.sqrt(-@value)))
end