From: "Simon Kröger" Date: 2006-08-08T05:53:08+09:00 Subject: Re: nextPowerOf2(n) Rick DeNatale wrote: > I hope that this doesn't offend anyone, but in the spirit of test > infection... > > I actually took the various suggestions and wrote test cases. Here's > my file "next_power_of_2.r b" which defines either methods on Integer, > or a class with a next_power_of_2() class method, following the > various suggestions, I did give these methods a consistent name > following Ruby naming standards. ... > This really isn't surprising, since floating point arithmetic only > approximates real arithmetic using real numbers. > > My original intention was to benchmark the various suggestions, but > since you can get the wrong answers with arbitrarily fast code, I > don't think that it matters much. Hmm, what about: def next_power_of_2(n) throw 'eeek' if n < 0 return 1 if n < 2 1 << (n-1).to_s(2).size end ? cheers Simon