From: Phrogz Date: 2007-02-13T06:25:08+09:00 Subject: Re: What's the correct and fast way to determine if a (gig) number is a perfect square? On Feb 12, 2:07 pm, "Sam Kong" wrote: > On Feb 12, 12:12 pm, Joel VanderWerf wrote: > > Wrong how? > > > irb(main):001:0> x = 55833579873437812 > > => 55833579873437812 > > irb(main):002:0> sqrt = Math::sqrt(x) > > => 236291303.0 > > irb(main):003:0> sqrt.floor**2 - x > > => -3 > > I think even if you use #round, the problem won't go away. > Float type cannot generate correct result due to its limited > precision. For example: irb(main):004:0> x=981723462487562983749812734972342334123435635465656432452 => 981723462487562983749812734972342334123435635465656432452 irb(main):005:0> y=x**2 => 963780956798569484937549463180492938080866374138542452022484415498543617865176348383792466015930367123924038732304 irb(main):006:0> z=Math.sqrt(y) => 9.81723462487563e+056 irb(main):007:0> z==x => true irb(main):010:0> z==(x+1000) => true irb(main):011:0> z==(x+10000) => true