Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float Domain Error

Just got a "FloatDomainError" in my app, with the message "NaN" where the server would ordinarily describe the offense. The server error page points me to these lines (well, the first one):

n = ((self.weight * (c.percent)/100) / c.package_weight).to_i
n.times do 

The problem seems to be with the .to_i, which is meant to convert what totals to 8.35 into an integer so I can do n.times do.

The problem is, if I take to_i away, I get an error telling me, logically enough, that there's no method "times" for the float 8.35. But I don't get why it's not letting me round off 8.35 to an integer. I've tried .floor too, but I get the FloatDomainError.

Ideas on how to round this off so I can get it to work?

Thanks!

like image 723
Sasha Avatar asked Oct 03 '12 08:10

Sasha


1 Answers

This error means that you tried to convert a NaN value to int. NaN stands for "Not a number". That probably meants that your calculation is flawed and not result in 8.35. Maybe the value of c.package_weight is zero and you divide with it.

like image 104
Matzi Avatar answered Oct 13 '22 14:10

Matzi