Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

32651:ERROR comparison of Float with Float failed ruby

I'm new to rails, and I'm getting an error and I can't seem to find were the problem is. Here is the log:

[32651:ERROR] 2012-10-09 13:46:52 :: comparison of Float with Float failed
[32651:ERROR] 2012-10-09 13:46:52 :: /home/sunny/backend/lib/analytics/lifetime.rb:45:in `each'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max_growth'
/home/sunny/backend/lib/analytics/lifetime.rb:15:in `run'
/home/sunny/backend/lib/analytics/post_analytics.rb:102:in `lifetime'
/home/sunny/backend/lib/analytics/post_analytics.rb:27:in `run`run'
lib/endpoints/posts/index.rb:65:in `block in response'
lib/endpoints/posts/index.rb:62:in `each'
lib/endpoints/posts/index.rb:62:in `response'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/api.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validation/required_param.rb:43:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:61:in `block in call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validator.rb:40:in `safely'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:59:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/async_middleware.rb:73:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:114:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:91:in `block in new'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `block in process'

Whenever I go to the file and find that row, there is no each, and I can't understand where the problem is. Can anyone help? More than a fix, I want to understand the problem.

Here is the file:

class Lifetime
  attr_reader :values

  GROWTH_CUT_OFF = 0.10

  def initialize(engaged_users_values)
    @values = engaged_users_values
  end

  def run
    growths      = growth(values)

    return [0] if growths.uniq.first == 0 || growths.empty?

    max_growth   = max_growth(growths)
    dead_growth  = least_growth(growths, max_growth)

    time_diff = time_diff(values.last['end_time'],
                          values.first['end_time'])
    return [time_diff] unless dead_growth

    dead_loc     = growths.index(dead_growth)
    dead_value   = values[dead_loc]
    lifetime(values.first, dead_value)
  end

  def time_diff(last, first)
    in_minutes(parse_time(last)-parse_time(first))
  end

  def parse_time(time)
    DateTime.parse(time)
  end

  def growth(values)
    values.each_cons(2).map do |one, two|
      one_value = one['value'].to_f
      two_value = two['value']

      ((two_value-one_value)/(one_value)*100).round(2)
    end
  end

  def max_growth(growths)
    growths.max
  end

  def least_growth(growths, max_growth)
    growth_cut = growth_cut(max_growth)
    growths.select {|g| g <= growth_cut}.first
  end

  def growth_cut(max_growth)
    max_growth*GROWTH_CUT_OFF
  end

  def lifetime(first_value, last_value)
    first = parse_time(first_value['end_time'])
    last  = parse_time(last_value['end_time'])

    result = last-first
    [in_minutes(result)]
  end

  def in_minutes(time)
    time.to_f*24*60
  end
end
like image 329
Yair Villar Avatar asked Oct 09 '12 14:10

Yair Villar


1 Answers

If one of both Floats is NaN then you get this message. So, prevent the NaN case.

We can give you more info if you post the exact code of the error (the max_growth method)

like image 147
Hugo Logmans Avatar answered Oct 21 '22 09:10

Hugo Logmans