0

I am trying to solve a study problem with Pi in Python. The task is to print N-decimal numbers of Pi. When using the decimal library, everything goes smoothly, but we cannot use that :( At this moment I can only return 15 decimals. Please, can you explain to me why it stops at 15 and how to return more then 15? Many thanks.

def calc_pi(n):
    pi = 0
    k = 0

    for k in range(n):
        pi += (16**(-k)) * ((4/(8*k + 1)) - (2/(8*k + 4)) - (1/(8*k + 5)) - (1/(8*k + 6)))
    
    return str(pi)

print(calc_pi(100))


>>> 3.141592653589793
Martin
  • 1
  • 1
  • "At this moment I can only return 15 decimals. Please, can you explain to me why it stops at 15" Because that's how much the `float` type supports. "how to return more then 15?" The natural way is with Decimal. Otherwise, by implementing something like Decimal yourself. We can't see your assignment, so there's no way we can guess how you were intended to approach it. – Karl Knechtel Feb 10 '23 at 13:49
  • @KarlKnechtel Do any of the original's answers really apply here? I don't think so. – Kelly Bundy Feb 10 '23 at 13:50
  • Since all of the math done here is with basic arithmetic on integers (addition/multiplication/subtraction/division), `fractions.Fraction` should also work. – Karl Knechtel Feb 10 '23 at 13:52
  • @SergeBallesta "other equivalent external modules" (or hand-rolling it) are the only sensible approaches, though. That's the canonical, anyway, and if anything else made sense, the answer would belong *there*. – Karl Knechtel Feb 10 '23 at 13:53
  • @KarlKnechtel: If decimal is forbidden, I would assume that OP is expected to understand the underlying problem and propose a solution. Using fractions (which is fundamentally an extension of decimals) cannot be the correct way. – Serge Ballesta Feb 10 '23 at 13:55
  • You can use `Decimal` to get precision more than 15 decimals. – Vitalii Volkov Feb 10 '23 at 13:56
  • `from decimal import Decimal, getcontext` Wrap all numbers into `Decimal` and also set precision like this: `getcontext().prec = 100` – Vitalii Volkov Feb 10 '23 at 13:59
  • @KarlKnechtel: Other approaches could include a hand coded fixed precision using the standard Python integer type which is a natively multi-precision type. I do agree that it is just trying to re-invent the wheel when the decimals module (and a ton of equivalent ones) exists. But it can make sense to have students learn the hard way... – Serge Ballesta Feb 10 '23 at 14:00
  • @SergeBallesta right; so it's implementing (the necessary functionality of) `Decimal`. Everything will fundamentally boil down to that. If that's the actual question, then we should be clear and explicit about it. – Karl Knechtel Feb 10 '23 at 14:01
  • @Martin for a comparison you can use `sympy.pi.evalf(amount_of_digits)`, [live shell](https://www.sympy.org/en/shell.html) – cards Feb 10 '23 at 14:03
  • 1
    @VitaliiVolkov Why do you tell them something they already know and which they told us they can't use? – Kelly Bundy Feb 10 '23 at 14:04
  • @KarlKnechtel: I cannot be sure either ;-) , only OP can... The sentence that make me think it could be what it actually asked is *When using the decimal library, everything goes smoothly, but we cannot use that :(* in the question. BTW, and IMHO that sentence alone is enough to prevent any answer using the decimal library to be acceptable... – Serge Ballesta Feb 10 '23 at 14:04
  • @SergeBallesta Yes, that sentence (and I think that's the "catch" the title refers to) is why I don't think any of the duplicate original's answers apply. – Kelly Bundy Feb 10 '23 at 14:16

0 Answers0