Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accuracy of sqrt
#1
I'm trying to create a function which calculate root using the Newton method, but with small numbers this doesn't work. Is it problem with Python or with my func? I have tried Decimal, __future__ division, nothing helped.
def square_root(num):
    x1 = (num * 1.0) / 2.0
    x2 = (x1 + (num / x1)) / 2.0
    while abs(x1 - x2) >= num:
        x1 = x2
        x2 = (x1 + (num / x1)) / 2.0
    return x2


print(square_root(4))
print(square_root(4e-128))
Error:
2.0 2.0000000000000003e-64
Reply
#2
No it is not a problem with python, nor with your function. It comes from the fact that the computer handles real number with a finite precision. Here is a code to show you which bits are stored in the 64 bits floating point numbers used by python
import struct

def float_to_int64(x):
    """Convert a python float to a 64 bits integer.
    
    Convert a float to an integer representing this
    float in the 64 bits ieee754 double precision format."""
    
    u, v = struct.unpack(">LL", struct.pack(">d", x))
    return (u << 32) | v

def ieee754(x):
    """Returns a representation of the ieee754 of a floating number
    """
    s = '{:0>64}'.format(bin(float_to_int64(x))[2:])
    return ' '.join((s[0], s[1:12], s[12:]))

def square_root(num):
    x1 = (num * 1.0) / 2.0
    x2 = (x1 + (num / x1)) / 2.0
    while abs(x1 - x2) >= num:
        x1 = x2
        x2 = (x1 + (num / x1)) / 2.0
    return x2
 
 
x = 2e-64
y = square_root(4e-128)

print(ieee754(x))
print(ieee754(y))
print('x - y', x - y)
The result is
Output:
0 01100101011 0101000011111111110101000100111101001010011100111101 0 01100101011 0101000011111111110101000100111101001010011100111110 x - y -3.3735033418337674e-80
Your result differs from the best machine approximation of 2e-64 by only -3e-80. As you can see, it differs only on the last bit in machine representation.

You need to read and understand this documentation page
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  number accuracy problem? roym 5 1,838 Dec-24-2021, 07:57 AM
Last Post: roym
  Calculating accuracy on RNN-LSTM model hobbyist 0 6,126 May-14-2021, 08:55 AM
Last Post: hobbyist
  custom sqrt bluefrog 4 3,118 Jul-14-2019, 08:49 PM
Last Post: ichabod801
  from 50 random numbers printing only the sqrt(...)==0 AFKManager 1 2,422 Jan-12-2019, 08:16 PM
Last Post: ichabod801
  math.sqrt / pow() rounding small floats, workaround? robertHTompkins 7 3,962 Dec-22-2018, 10:58 PM
Last Post: robertHTompkins
  Measure accuracy of Object Detection Shameendra 2 2,687 Nov-19-2018, 01:04 PM
Last Post: Shameendra
  why not accuracy working rajeev1729 1 2,614 Sep-12-2017, 02:15 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020