how to increase number of floating points in python

Question:

I need to calculate 100 floating points of phi φ

it’s defeat showing only 15 floating points

but I need 100 of floating points.is there an easy way rather than implement pHi from beginning

Asked By: Dileesha abilash

||

Answers:

If you want more numbers after decimal use decimal module


from decimal import Decimal, setcontext, Context

setcontext(Context(prec=100))

a = Decimal( 22/7)

print(a)
# 3.142857142857142793701541449991054832935333251953125

# @ Lutz Lehmann comment

a=Decimal(22)/7
print(a)
# 3.142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857143

Try this

you can get the 100 or more digits of pi using mpmath

from mpmath import mp


print("100 digits")
mp.dps = 100  # set number of digits
print(mp.pi)

print("n200 digits")
mp.dps = 200  # set number of digits
print(mp.pi)

print("n500 digits")
mp.dps = 500  # set number of digits
print(mp.pi)

print("n1000 digits")
mp.dps = 1000  # set number of digits
print(mp.pi)

OUTPUT


100 digits
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

200 digits
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382

500 digits
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491

1000 digits
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199

GUI based

from customtkinter import *
from tkinter import StringVar
from mpmath import mp
from threading import Thread


def generatePi():
    btn._state = "Disabled"
    root.focus_force()
    def inner():
        try:
            digit = int(var.get())
        except ValueError:
            pass
        else:
            mp.dps = digit
            text.delete(1.0, END)
            text.insert(1.0, mp.pi)
        finally:
            btn._state = "normal"


    Thread(target=inner).start()
    

def selectAll(e):
    text.tag_add(SEL, "1.0", END)
    text.mark_set(INSERT, "1.0")

set_appearance_mode("dark")
set_default_color_theme("green")

root = CTk()
root.geometry("900x700+300+100")

root.resizable(False, False)

CTkLabel(root, text="Pi Constant Generator", font=CTkFont("Comic Sans MS", 40, "normal")).grid(column=0, row=0, columnspan=100, pady=(30, 50))
text = CTkTextbox(master=root, width=500, height=500)
text.grid(column=0, row=1, padx = 200, columnspan=4)


var = StringVar(value=100)
CTkEntry(master=root,width=80, textvariable=var).grid(column=0, row=3, padx = (200, 0), pady=(10, 70))

btn = CTkButton(master=root, command=generatePi, text="Generate")
btn.grid(column=1, row=3, pady=(10, 70))

text.bind("<FocusIn>", selectAll)
root.mainloop()


PREVIEW

enter image description here

Answered By: codester_09

For your problem you can use this code, maybe it’s more simple :

a = 22/7

# give 100 digits after the point
a = format(a, '.100f')   
Answered By: ComputingVictor
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.