0
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.)

kimking
  • 7
  • 1
  • Yes, it is generally true that we avoid bit shifting with floating-point numbers as it is complex compared to integer values. Floating-point numbers have a specific representation, including a mantissa and an exponent. Shifting the bits of a floating-point number requires a deep understanding of its internal structure and may involve additional operations such as masking or adjusting the exponent. Consequently, it may be more computationally expensive or require specialized handling. – Florian Fasmeyer May 17 '23 at 06:06
  • Learn more about floating-point bitwise shift here: https://stackoverflow.com/questions/42110536/bit-shifting-in-python – Florian Fasmeyer May 17 '23 at 06:19

1 Answers1

0

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

Florian Fasmeyer
  • 795
  • 5
  • 18
  • If your question gets closed by an admin, DO NOT DELETE IT! The Overflow automated system penalizes people for closing questions for which time was spent to answer. It is better not to touch it and wait until you can create a new question. Good luck with StackOverflow! – Florian Fasmeyer May 17 '23 at 06:16