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.

Pinteco
  • 247
  • 1
  • 8
  • 2
    Why are you asking yourself a question in the comments @Pinteco? – Felipe Sulser Jun 21 '19 at 08: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. – Pinteco Jun 21 '19 at 08:20
  • 1
    Possible duplicate of [Print pi to a number of decimal places](https://stackoverflow.com/questions/45416626/print-pi-to-a-number-of-decimal-places), specifically [this answer](https://stackoverflow.com/a/45416807). – mkrieger1 Jun 21 '19 at 08:30
  • See also [this answer](https://stackoverflow.com/a/43753300) and https://stackoverflow.com/questions/588004/is-floating-point-math-broken – mkrieger1 Jun 21 '19 at 08:32
  • @mkrieger1 pi here is just an example. I want any number with any precision. But I will check the links. – Pinteco Jun 21 '19 at 08:32
  • 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. – mkrieger1 Jun 21 '19 at 08:33
  • "Is math.pi from python reliable?" - what does "reliable" mean? – Bogdan Doicin Jun 21 '19 at 08:41
  • @BogdanDoicin Can I trust it? if the precision is small it will affect my calculations. – Pinteco Jun 21 '19 at 08:52
  • 1
    There's a nice article about this topic: https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/ But it probably depends on the specific calculations you do. – mkrieger1 Jun 21 '19 at 08:55

4 Answers4

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)
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • 1
    `print (format (pi,'.100f'))` gives me 50 decimals accuracy followed by 50 zeroes. `mp.pi` does work, however. – Jim Eisenberg Jun 21 '19 at 08:35
  • 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. – Bogdan Doicin Jun 21 '19 at 08:36
0

Another possibility:

import math
from decimal import getcontext, Decimal

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

print(number)
bene
  • 798
  • 7
  • 19
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.

srka
  • 792
  • 8
  • 20
-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)
Felipe Sulser
  • 1,185
  • 8
  • 19
  • 1
    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. – mkrieger1 Jun 21 '19 at 08:29
  • `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. – Felipe Sulser Jun 21 '19 at 08:31