Class: ProportionBoundedDivideInstruction

Inherits:
Instruction
  • Object
show all
Defined in:
lib/instructions/proportion/proportion_bounded_divide.rb

Overview

pops the top 2 items of the :proportion stack; pushes a ValuePoint with their quotient onto the :proportion stack, bounded by [0.0,1.0]

note: the top item is the denominator, the second item is the numerator

note: will push an :error ValuePoint instead of the expected result if the numerator is 0.0

needs: 2 :proportion

pushes: 1 :proportion

Instance Attribute Summary

Attributes inherited from Instruction

#context

Instance Method Summary collapse

Methods inherited from Instruction

all_instructions, #go, inherited, #initialize, #needs, #pushes, to_nudgecode

Constructor Details

This class inherits a constructor from Instruction

Instance Method Details

#cleanupObject



29
30
31
# File 'lib/instructions/proportion/proportion_bounded_divide.rb', line 29

def cleanup
  pushes :proportion, @result
end

#deriveObject



21
22
23
24
25
26
27
28
# File 'lib/instructions/proportion/proportion_bounded_divide.rb', line 21

def derive
  if @arg2 != 0.0
    quotient = [@arg1/@arg2, 1.0].min
    @result = ValuePoint.new("proportion", quotient)
  else
    raise NaNResultError, "#{self.class} cannot divide by 0.0"
  end
end

#preconditions?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/instructions/proportion/proportion_bounded_divide.rb', line 14

def preconditions?
  needs :proportion, 2
end

#setupObject



17
18
19
20
# File 'lib/instructions/proportion/proportion_bounded_divide.rb', line 17

def setup
  @arg2 = @context.pop_value(:proportion)
  @arg1 = @context.pop_value(:proportion)
end