2012-10-09 42 views
5

我是新來的rails,並且出現錯誤,我似乎無法找到問題所在。下面是日誌:32651:Float與Float失敗ruby的錯誤比較

[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' 

每當我去的文件,並找到該行,沒有each,我不明白問題出在哪裏。誰能幫忙?不僅是修復,我想了解這個問題。

以下是文件:

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 

回答

12

如果兩個浮標的一個是NaN,那麼你得到這個消息。所以,防止NaN的情況。

我們可以給你更多的信息,如果你發佈錯誤的確切代碼(max_growth法)

+0

高清增長(值) values.each_cons(2).MAP做|一,二| one_value =一個[ '值']。to_f two_value = 2 [ '值'] 如果one_value == NaN的 one_value == 0 別的 ((two_value-one_value)/(one_value)* 100).round (2) 端 端 端 DEF max_growth(贅生物) growths.max 端 –

+0

這是不可讀。創建一個新問題,詢問如何解決這個具體問題,因爲沒有人會看到已經回答的問題。 –