SHIFT23 = (1 << 23).to_f INV_SHIFT23 = 1.0 / SHIFT23 def fast_pow(a, b) x = a.int_bits x *= INV_SHIFT23 x -= 0x7f y = x - (x >= 0 ? x.to_i : x.to_i - 1) b *= x + (y - y * y) * 0.346607 y = b - (b >= 0 ? b.to_i : b.to_i - 1) y = (y - y * y) * 0.33971 Float.get_from_int_bits(((b + 0x7f - y) * SHIFT23).to_i) end class Float def int_bits [self].pack('g').unpack('B*').first.to_i(2) end class << self def get_from_int_bits(i) [sprintf("%032b", i)].pack('B*').unpack('g').first end end end def times(n, &block) time = Time.now n.times(&block) Time.now - time end puts(times(100000000) { fast_pow(1.5, 3.0) }) puts(times(100000000) { 1.5 ** 3.0 })