Sha256: a7ccf153ab55060a8f8f97b106279b4c47538f2cf2cd46aebb47a72dc817590c

Contents?: true

Size: 849 Bytes

Versions: 2

Compression:

Stored size: 849 Bytes

Contents

module Keepit
  # A float value which decays exponentially toward 0 over time.
  class Decaying
    attr_accessor :e
    attr_accessor :p

    # opts - Hash
    #        :p - Float (0.0) The initial value
    #        :e - Float (Math::E) Exponent base
    #        :r - Float (Math.log(0.5) / 10) Timescale factor - defaulting to decay 50% every 10 seconds
    def initialize(opts = {})
      @p = opts[:p] || 0.0
      @e = opts[:e] || Math::E
      @r = opts[:r] || Math.log(0.5) / 10
      @t0 = Time.now.to_i
    end

    # Add to current value
    #
    # d - Float value to add
    def <<(d)
      @p = value + d
    end

    # Returns Float the current value (adjusted for the time decay)
    def value
      return 0.0 unless @p > 0
      now = Time.now.to_i
      dt = now - @t0
      @t0 = now
      @p *= @e**(@r * dt)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
keepit-0.1.1 lib/keepit/decaying.rb
keepit-0.1.0 lib/keepit/decaying.rb