how do you use bit swift in python

37 Views Asked by At
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)

I made newton-raphson method with python. Is there any way to make it without using '/'? (I have considered making it with bit shift but a heard that bit shift can only work with integers.)

1

There are 1 best solutions below

1
Florian Fasmeyer On

Bitwise shifting in Python is done as follows:

x = 5
x << 1   # Shift once.
print(x) # Displays 10! (x*2)

You should search on Google for such questions; it takes time for people to answer on StackOverflow. Save your time:

enter image description here