From: "Jesús Gabriel y Galán" Date: 2010-02-17T03:40:48+09:00 Subject: Re: ruby to_i problem On Tue, Feb 16, 2010 at 7:16 PM, Phil Cooper-king wrote: > I'm having an issue with a float being converted to an int, > > The maths is part of a vector rotation of a little video game, which x,y > coords are rotated at a given angle. > > For what ever reason the first instance gives an error, and every > instance there after works as it should > > This cant be right? > > > ruby 1.8.7 > [irb] > # This works the way I would think >>> 1.0.o_i > => 1 > > > # This doesn't >>> x = -1 >>> y = -1 >>> angle = 90 * Math::PI / 180 >>> n_x = (Math::cos(angle) * x) - (Math::sin(angle) * y) > => 1.0 > > > # this is where its werid >>> n_x.to_i > => 0 > [/irb] > > any further 1.0.to_i works You are facing a problem about the inaccuracy inherent to floating point numbers: irb(main):001:0> 0.9999.to_i => 0 irb(main):002:0> x = -1 => -1 irb(main):003:0> y = -1 => -1 irb(main):004:0> angle = 90 * Math::PI / 180 => 1.5707963267949 irb(main):005:0> n_x = (Math::cos(angle) * x) - (Math::sin(angle) * y) => 1.0 irb(main):009:0> "%.20f" % n_x => "0.99999999999999988898" irb(main):010:0> irb(main):010:0> n_x => 1.0 n_x is very close to 1.0 but it's not. to_i rounds the number down, so you get 0. You can check the archives for a more thorough explanation about this, there's been plenty of discussion in the past. Jesus.