pi

Why does math.cos(math.pi/2) not return zero?

Why does math.cos(math.pi/2) not return zero? Question: I came across some weird behavior by math.cos() (Python 3.11.0): >>> import math >>> math.cos(math.pi) # expected to get -1 -1.0 >>> math.cos(math.pi/2) # expected to get 0 6.123233995736766e-17 I suspect that floating point math might play a role in this, but I’m not sure how. And if …

Total answers: 3

how to increase number of floating points in python

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 || Source Answers: If you want more …

Total answers: 2

Continued fraction for pi in Python

Continued fraction for pi in Python Question: I’m trying to approximate pi using continued fraction. I’m using this formula. After many hours, I ended up here. for i in range(1,15,1): e = ((2*i – 1) ** 2) b = (e / (6 + ((2*(i+1) – 1) ** 2))) print(3+b) ` But my output is not …

Total answers: 2

How can I make an output in radians with pi in it?

How can I make an output in radians with pi in it? Question: I want to give out an angle in radians in python. I also know how to do that but is it posible to make the output like that 1 * pi instead of 3.14159 without programming it myself? Thx for help. Asked …

Total answers: 2

Cannot import name '_gi'

Cannot import name '_gi' Question: I’m trying to add a repository to ppa with the add-apt-repository commands but the _gi module from Python is not found. I did this command : sudo add-apt-repository ppa:s-mankowski/ppa-kf5 Here is the traceback : Traceback (most recent call last): File “/usr/bin/add-apt-repository”, line 11, in <module> from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler …

Total answers: 6

Why does Python's hash of infinity have the digits of π?

Why does Python's hash of infinity have the digits of π? Question: The hash of infinity in Python has digits matching pi: >>> inf = float(‘inf’) >>> hash(inf) 314159 >>> int(math.pi*1e5) 314159 Is that just a coincidence or is it intentional? Asked By: wim || Source Answers: _PyHASH_INF is defined as a constant equal to …

Total answers: 3

How convert user inputted constant (pi,e) to float in python?

How convert user inputted constant (pi,e) to float in python? Question: I’m writing a code which must compute definite integral of a function. I’ll provide code in the below. How can I urge computer to understand user input “pi” or “e” for constant numbers? Problem is that I must convert the type of input to …

Total answers: 2

Multiprocessing code to calculate pi never finishes

Multiprocessing code to calculate pi never finishes Question: I want to calculate pi. The procedure is simple: Make a 1×1 square and draw a circle in the square. Then divide by 4. Take two random values (x, y). If x2 + y2 ≤ 1 than the point is in the circle. Repeat the above N …

Total answers: 3

BBP-Algorithm in Python – Working with Arbitrary-precision arithmetic

BBP-Algorithm in Python – Working with Arbitrary-precision arithmetic Question: I am writing on a term paper about the calculation of pi. While i have finished the theoretical site, i`m now struggeling implementing the BBP-Algorithm in Python. You can find the BBP-Algorithm here: http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula And this is my Implementation in Python: from sympy.mpmath import * pi …

Total answers: 2

Python Pi approximation

Python Pi approximation Question: So I have to approximate Pi with following way: 4*(1-1/3+1/5-1/7+1/9-…). Also it should be based on number of iterations. So the function should look like this: >>> piApprox(1) 4.0 >>> piApprox(10) 3.04183961893 >>> piApprox(300) 3.13825932952 But it works like this: >>> piApprox(1) 4.0 >>> piApprox(10) 2.8571428571428577 >>> piApprox(300) 2.673322240709928 What am …

Total answers: 6