class Riak::Client::Decaying

A float value which decays exponentially toward 0 over time. @private

Attributes

e[RW]
p[RW]

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts options @option options [Float] :p (0.0) The initial value @option options [Float] :e (Math::E) Exponent base @option options [Float] :r (Math.log(0.5) / 10) Timescale

factor - defaulting to decay 50% every 10 seconds
# File lib/riak/client/decaying.rb, line 14
def initialize(opts = {})
  @p = opts[:p] || 0.0
  @e = opts[:e] || Math::E
  @r = opts[:r] || Math.log(0.5) / 10
  @t0 = Time.now
end

Public Instance Methods

<<(d) click to toggle source

Add to current value. @param [Float] d the value to add

# File lib/riak/client/decaying.rb, line 23
def <<(d)
  @p = value + d
end
value() click to toggle source

@return [Float] the current value (adjusted for the time decay)

# File lib/riak/client/decaying.rb, line 28
def value
  now = Time.now
  dt = now - @t0
  @t0 = now
  @p = @p * (@e ** (@r * dt))
end