1

I am a beginner in Python. I try to evaluate pi in Python Language. I use here the Archimedes method.

My code is:

import math
from decimal import *
getcontext().prec = 50

pi = 0
PolySides = 6
SideLen = 1
Perim = 6
Dia = 2

while PolySides < 10000000000:
    SideLen2 = Decimal(SideLen) / Decimal(2)
    radius_a = Decimal(math.sqrt(1 - SideLen2**2))
    radius_b = Decimal(1) - Decimal(radius_a)
    SideLenNew = Decimal(math.sqrt(SideLen2**2 + radius_b**2))
    PolyCircum = Decimal(PolySides * SideLen)
    pi = PolyCircum / Dia
    print('Polygon Sides:', PolySides, 'Pi = ', pi)
    SideLen = SideLenNew
    PolySides = 2 * PolySides

But for some reason this code prints pi upto 14 decimal places. I cannot understand where I'm losing precision. Thank you.

6
  • Interesting question. However, it might fit better on codereview. Commented Aug 29, 2018 at 11:16
  • 6
    @Mathieu Since the code does not yet work as intended, no, it does not fit better on Code Review. Commented Aug 29, 2018 at 11:17
  • 1
    I can't reproduce, using your code as-is I am getting a value of pi 49 decimal places long. Commented Aug 29, 2018 at 11:19
  • 1
    @Zinki...My code returns pi =3.1415926535897942262209880937007255852222442626953......which is actually right upto 14 Decimal places Commented Aug 29, 2018 at 11:21
  • @IndrajitGhosh ah well that might just be up to floating point arithmetic. standard Floating point types only have 53 bits of precision, which comes out to around 14-15 decimal places. Commented Aug 29, 2018 at 11:22

3 Answers 3

5

You are weaving in and out between float and decimal. math.sqrt takes a float and returns a float, so there'll be conversion where your precision is truncated to float's.

These changes suffice:

while PolySides < 10000000000000000000:
    SideLen2 = Decimal(SideLen) / 2
    radius_a = (1 - SideLen2**2).sqrt()
    radius_b = 1 - radius_a
    SideLenNew = (SideLen2**2 + radius_b**2).sqrt()
    PolyCircum = Decimal(PolySides * SideLen)
    pi = PolyCircum / Dia
    print('Polygon Sides:', PolySides, 'Pi = ', pi)
    SideLen = SideLenNew
    PolySides = 2 * PolySides
Sign up to request clarification or add additional context in comments.

2 Comments

@IndrajitGhosh: You could use Decimal's sqrt method to fix this problem.
@unutbu...Thank you ...It fixes my problem.
2

You must use the .sqrt() method of your decimals:

from decimal import *
getcontext().prec = 50

pi = 0
PolySides = 6
SideLen = 1
Perim = 6
Dia = 2

while PolySides < 1000000000000:
    SideLen2 = Decimal(SideLen) / Decimal(2)
    radius_a = (1 - SideLen2**2).sqrt()
    radius_b = Decimal(1) - Decimal(radius_a)
    SideLenNew = (SideLen2**2 + radius_b**2).sqrt()
    PolyCircum = Decimal(PolySides * SideLen)
    pi = PolyCircum / Dia
    print('Polygon Sides:', PolySides, 'Pi = ', pi)
    SideLen = SideLenNew
    PolySides = 2 * PolySides

Output:

Polygon Sides: 824633720832 Pi =  3.1415926535897932384626357839340399511415560167917

has 22 exact decimals.

Comments

1

Make sure the variable pi is itself a Decimal too, and avoid using math.sqrt() as @Amadan wrote. Use Decimal.sqrt()

import math
from decimal import *
getcontext().prec = 50

pi = Decimal(0)
PolySides = 6
SideLen = 1
Perim = 6
Dia = 2

while PolySides < 10000000000:
    SideLen2 = Decimal(SideLen) / Decimal(2)
    radius_a = Decimal((Decimal(1) - SideLen2**2).sqrt())
    radius_b = Decimal(1) - Decimal(radius_a)
    SideLenNew = Decimal((SideLen2**2 + radius_b**2).sqrt())
    PolyCircum = Decimal(PolySides * SideLen)
    pi = PolyCircum / Dia
    print('Polygon Sides:', PolySides, 'Pi = ', pi)
    SideLen = SideLenNew
    PolySides = 2 * PolySides

Gives:

Polygon Sides: 6 Pi =  3
Polygon Sides: 12 Pi =  3.1058285412302491481867860514885799401888268158392
Polygon Sides: 24 Pi =  3.1326286132812381971617494694917362446497769154815
Polygon Sides: 48 Pi =  3.1393502030468672071351468212084211891503505893626
Polygon Sides: 96 Pi =  3.1410319508905096381113529264596601070364122161628
Polygon Sides: 192 Pi =  3.1414524722854620754506093089612256452476623045496
Polygon Sides: 384 Pi =  3.1415576079118576455164633451298595415043764795884
Polygon Sides: 768 Pi =  3.1415838921483184086689696037211533505200449157810
Polygon Sides: 1536 Pi =  3.1415904632280500957384585059309517235542823086758
Polygon Sides: 3072 Pi =  3.1415921059992715505447766406101173531274972549662
Polygon Sides: 6144 Pi =  3.1415925166921574475928740847688319059677188923680
Polygon Sides: 12288 Pi =  3.1415926193653839551895493120653190422221927255946
Polygon Sides: 24576 Pi =  3.1415926450336908966721415089192384127226230112419
Polygon Sides: 49152 Pi =  3.1415926514507676517042536404922190204484274723852
Polygon Sides: 98304 Pi =  3.1415926530550368416911231804154742022572768058866
Polygon Sides: 196608 Pi =  3.1415926534561041392646431596150783313543414874292
Polygon Sides: 393216 Pi =  3.1415926535563709636628233165541133642749135041839
Polygon Sides: 786432 Pi =  3.1415926535814376697626683659225751788703495073714
Polygon Sides: 1572864 Pi =  3.1415926535877043462876483788980471857502408672475
Polygon Sides: 3145728 Pi =  3.1415926535892710154188945540564999738004063788314
Polygon Sides: 6291456 Pi =  3.1415926535896626827017061710907747199859793791592
Polygon Sides: 12582912 Pi =  3.1415926535897605995224090799271347533561151459628
Polygon Sides: 25165824 Pi =  3.1415926535897850787275848074223367208751396830886
Polygon Sides: 50331648 Pi =  3.1415926535897911985288787393140192102034265840860
Polygon Sides: 100663296 Pi =  3.1415926535897927284792022222880574573760314838880
Polygon Sides: 201326592 Pi =  3.1415926535897931109667830930316368707217160322736
Polygon Sides: 402653184 Pi =  3.1415926535897932065886783107175360897801705020852
Polygon Sides: 805306368 Pi =  3.1415926535897932304941521151390111674024112028327
Polygon Sides: 1610612736 Pi =  3.1415926535897932364705205662443799538615730707255
Polygon Sides: 3221225472 Pi =  3.1415926535897932379646126790207221515422136434928
Polygon Sides: 6442450944 Pi =  3.1415926535897932383381357072148077010289894182968

Comments

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.