
# errDigits.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
Investigate how quickly Newton-Raphson approaches a 
square root when the starting value has different error values. 

This code does not find the square root, but instead measures the
change in the error value in terms of the number of correct
digits it represents, and the number of iterations.

'''
import math

errVal = 9   # test values: 0.27   4  9 
print("Initial Error:", errVal) 

numDigits = -math.log10(errVal)   
   # number of correct digits in the error
numIters = 0
print("Iter  Error       No. Digits")
print(f"{numIters:2d}    {errVal:.3e}   {numDigits:.3f}")

while numDigits <= 15:
  errVal = (errVal*errVal)/(2*(1+errVal))
  numDigits = -math.log10(errVal)
  numIters += 1
  print(f"{numIters:2d}    {errVal:.3e}   {numDigits:.3f}")
