I calculated it by mistake and found a complex number value.

Asked 2 years ago, Updated 2 years ago, 19 views

for i in range(1, 100):
    num = 2 ** i
    p2 = 0

    for k in range(1, num + 1):
            p2 += (1 / num + 1) * ( (1 - (k / num + 1) ** 2) ** .5 ) * 4

    print(p2)

This is the code, but the value should be close to the approximation of the pie value, but it keeps printing complex values.

for i in range(1, 100):
    num = 2 ** i
    p2 = 0

    for k in range(1, num):
            p2 += (1 / num) * ( (1 - (k / num) ** 2) ** .5 ) * 4

    print(p2)

On the other hand, this code works well.

Where did the problem come from?

python

2022-09-22 13:59

1 Answers

(1 - (k / num + 1) ** 2) ** .5 where (1 - (k/num + 1) ** 2) is negative.


2022-09-22 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.