Advertisement
Abhisek92

sqrt.py

May 6th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.30 KB | None | 0 0
  1. import struct
  2.  
  3. def sqrt(x):
  4.     x = float(x)
  5.     y = struct.pack('>f', x)
  6.     i = struct.unpack('>i', y)[0]
  7.     i = 0x5f3759df - (i >> 1)
  8.     y = struct.pack('>i', i)
  9.     z = struct.unpack('>f', y)[0]
  10.     z = z * (1.5 - 0.5 * x * z * z)
  11.     return 1 / z # return round(1/z) for closest int
  12.  
  13.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement