import sympy as sp
import sys
import datetime
import decimal
start_time = datetime.datetime.now()
x = sp.symbols('x')
a = 1234123412341234
f_x = 1/x - a
fp_x = sp.diff(f_x) # 미분
x_init = sys.float_info.min # 2.2250738585072014e-308
Tolerance = 1e-6
x_tmp = x_init
while True:
a = f_x.subs(x,x_tmp)
b = fp_x.subs(x,x_tmp)
x_numeric = (x_tmp) - (a/b)
NR_error = f_x.subs(x, x_numeric)
if abs(NR_error) <= Tolerance :
print(x_tmp)
break
# x_numeric = x_tmp
x_tmp = x_numeric
# result = x_tmp * bc
print('num : ' , x_numeric)
end_time = datetime.datetime.now()
time = end_time - start_time
ms_elapsed_time = time.microseconds / 1000
print(ms_elapsed_time)
我用python制作了newton-raphson方法。有什么办法可以不使用'/'吗? (我考虑过使用位移位来实现它,但听说位移位只能用于整数。)