2

I saw here how to find the sqrt(2) up to 100 digits in python:

import decimal
number = decimal.Decimal(2)
precision = decimal.Context(prec = 100)
print (number.sqrt(precision))

I tested and worked fine. When I try to use this code to pi, the print doesn't work. I tried

print (number.(precision)), print (number(precision)), etc. How the last line should be?

import decimal
number = decimal.Decimal(math.pi)
precision = decimal.Context(prec = 100)
print (???)

I'm using netbeans. I can, however, print the sqrt(pi).

EDIT: Thanks, guys!

Python returns: 3.141592653589793-1-159979634685441851615905761718750

Wolfram returns 3.141592653589793-2-384626433832795028841971693993751

Only after 14 digits the answer's diverges. Is math.pi from python reliable?

Strange enough, but the sqrt(2) up to 1000 digits in python and wolfram gives the same answer.

9
  • 2
    Why are you asking yourself a question in the comments @Pinteco? Commented Jun 21, 2019 at 8:16
  • 4
    Lol, a user answered saying that for him the code worked, so I asked him what did he wrote to make it work. But he deleted the answer and now looks like I'm talking to myself. Commented Jun 21, 2019 at 8:20
  • 1
    Possible duplicate of Print pi to a number of decimal places, specifically this answer. Commented Jun 21, 2019 at 8:30
  • 1
    Then you need to have the number with arbitrary precision in the first place, i.e. have an algorithm to calculate it. Floating-point constants are limited in precision. Commented Jun 21, 2019 at 8:33
  • 1
    There's a nice article about this topic: jpl.nasa.gov/edu/news/2016/3/16/… But it probably depends on the specific calculations you do. Commented Jun 21, 2019 at 8:55

4 Answers 4

3

You can do what you want using format and specifying the number of places:

from math import pi
print (format (pi,'.100f'))

Another solution is by using the mpmath library:

from mpmath import mp
mp.dps = 100    # set number of digits
print(mp.pi)
Sign up to request clarification or add additional context in comments.

2 Comments

print (format (pi,'.100f')) gives me 50 decimals accuracy followed by 50 zeroes. mp.pi does work, however.
Yes, because math.pi and mp.pi come from two different libraries. mp is built for higher precision floating point numbers than math, so mp.pi has more decimals that math.pi. I had included the math.pi version for the cases in which less decimals are needed.
0

Another possibility:

import math
from decimal import getcontext, Decimal

getcontext().prec = 100
number = Decimal(math.pi)

print(number)

1 Comment

Setting getcontext().prec has no effect on the output. Still prints pi with 50 decimals.
0

Other answer are correct for printing with certain precision. However, if you try them with Decimal(math.pi) you will get 3.141592653589793115997963468544185161590576171875, obviously less than 100 digits, and even these digits are not all correct. Reason for this is that math.pi is float, which has limited precision.

To actually calcute correct digits of pi to any precision you can use this code (taken from official Python documentation for decimal library):

def pi():
    """Compute Pi to the current precision.

    >>> print(pi())
    3.141592653589793238462643383

    """
    getcontext().prec += 2  # extra digits for intermediate steps
    three = Decimal(3)      # substitute "three=3.0" for regular floats
    lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    getcontext().prec -= 2
    return +s               # unary plus applies the new precision

This presumes you have set getcontext().prec to wanted precision.

Comments

-1

If you just want to print the number with a certain precision do the following:

from decimal import Decimal
number = decimal.Decimal(math.pi)
print(format(number,'.100f'))

Otherwise, you may do the following to set the precision:

from decimal import getcontext, Decimal

getcontext().prec = 100
number = Decimal(math.pi)

print(number)

2 Comments

Except that math.pi as a float isn't accurate to an arbitrary number of decimal places, so this pretends to be accurate when it isn't.
math.pi was just copied from OP's original code. The question is about printing a number up to a certain number of decimal places and not whether math.pi is correct or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.