Skip to content

Comment on Fast Inverse Square Root

Comments

If anybody wants to further play around with this, I wrote the following script in python using matplotlib:

  import numpy
  import matplotlib.pyplot as plt
  import struct
  
  def power(x,p):
    i = struct.unpack("i", struct.pack("f", x))[0]
    i = (1-p) * 0x3f7a3bea + (p*i)
    return struct.unpack("f", struct.pack("i", i))[0]
  
  p = -0.5
  xs = numpy.arange(0.1,100,0.1)
  ys1 = map(lambda x: x**p, xs)
  ys2 = map(lambda x: power(x, p), xs)
  
  plt.plot(xs, ys1, label="Real function")
  plt.plot(xs, ys2, label="Approximation")
  
  plt.title("Graph of x^%d"%p)
  plt.legend(loc="upper left")
  plt.savefig("graph.png")
Change p to whatever you want (even values greater than 1) to see different powers.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.