pi

Print pi to a number of decimal places

爱⌒轻易说出口 提交于 2019-12-17 20:36:14
问题 One of the challenges on w3resources is to print pi to 'n' decimal places. Here is my code: from math import pi fraser = str(pi) length_of_pi = [] number_of_places = raw_input("Enter the number of decimal places you want to see: ") for number_of_places in fraser: length_of_pi.append(str(number_of_places)) print "".join(length_of_pi) For whatever reason, it automatically prints pi without taking into account of any inputs. Any help would be great :) 回答1: Why not just format using number_of

How is pi (π) calculated?

感情迁移 提交于 2019-12-17 18:03:22
问题 How can I write a function which will return pi (π) to a given number of decimal places? Speed is not a concern. I've been looking at http://bellard.org/pi/, but I still don't understand how to get the nth digit of pi. 回答1: In calculus there is a thing called Taylor Series which provides an easy way to calculate many irrational values to arbitrary precision. Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... (from http://www.math.hmc.edu/funfacts/ffiles/30001.1-3.shtml ) Keep adding those terms until the

How is pi (π) calculated?

巧了我就是萌 提交于 2019-12-17 18:02:13
问题 How can I write a function which will return pi (π) to a given number of decimal places? Speed is not a concern. I've been looking at http://bellard.org/pi/, but I still don't understand how to get the nth digit of pi. 回答1: In calculus there is a thing called Taylor Series which provides an easy way to calculate many irrational values to arbitrary precision. Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... (from http://www.math.hmc.edu/funfacts/ffiles/30001.1-3.shtml ) Keep adding those terms until the

Strange behaviour of macros C/C++

ⅰ亾dé卋堺 提交于 2019-12-17 14:58:26
问题 I'm using some macros, and observing some strange behaviour. I've defined PI as a constant, and then used it in macros to convert degrees to radians and radians to degrees. Degrees to radians works fine, but radians to degrees does not: piTest.cpp: #include <cmath> #include <iostream> using namespace std; #define PI atan(1) * 4 #define radians(deg) deg * PI / 180 #define degrees(rad) rad * 180 / PI int main() { cout << "pi: " << PI << endl; cout << "PI, in degrees: " << degrees(PI) << endl;

OverflowError: (34, 'Result too large')

丶灬走出姿态 提交于 2019-12-17 05:04:55
问题 I'am getting an overflow error(OverflowError: (34, 'Result too large') I want to calculate pi to 100 decimals here's my code: def pi(): pi = 0 for k in range(350): pi += (4./(8.*k+1.) - 2./(8.*k+4.) - 1./(8.*k+5.) - 1./(8.*k+6.)) / 16.**k return pi print(pi()) 回答1: Python floats are neither arbitary precision nor of unlimited size. When k = 349, 16.**k is much too large - that's almost 2^1400. Fortunately, the decimal library allows arbitrary precision and can handle the size: import decimal

C++ Pi approximation program

烂漫一生 提交于 2019-12-14 03:59:18
问题 I'm just learning C++ and so I have started to make a simple program to approximate the value of pi, using the series: Pi ^ 6 / 960 = 1 + 1 / 3 ^ 6 + 1 / 5 ^ 6... and so on continuing with denominators of odd numbers to the power of 6. Here is my code: /*------------------------------------------- * AUTHOR: * * PROGRAM NAME: Pi Calculator * * PROGRAM FUNCTION: Uses an iterative * * process to calculate pi to 16 * * decimal places * *------------------------------------------*/ #include

Write a program to estimate PI (π) using the Leibniz series in Java

谁说我不能喝 提交于 2019-12-13 13:24:09
问题 I've looked online for hours trying to see if I could find a solution and while I have found many solutions my instructions from my professor are as follows: Write a program to estimate PI (π) using the following series. This problem is also described in the text as problem 5.25 at the end of chapter 5. If you are unfamiliar with series, problem 5.24 is a single pass through a series and the solution is posted in the Homework 3 course module. π=4*(1-1/3+1/5-1/7+1/9-1/11+⋯〖-1〗^(i+1)/(2i-1))

Writing a Python function to calculate Pi

我只是一个虾纸丫 提交于 2019-12-13 02:13:22
问题 newbie here: Just learning Python and this one has me pooped. It's coming up with a function for manually computing Pi, the Madhava way. - Also known as exercise #16 from here: http://interactivepython.org/courselib/static/thinkcspy/Functions/thinkcspyExercises.html Can somebody take a look at my discombobulated and overly complex code and tell me if I'm missing something? Much thanks. (look at the equation on the wiki page first, otherwise my code will make no sense - well, it still may not.

find the value of pi till 50 digits

▼魔方 西西 提交于 2019-12-12 18:33:18
问题 I want to calculate the value of PI till 50 digits. How to do this in java for 50 decimal places? 回答1: You cant do that with default data types, as you need for 50 digits: 50 / log(2) * log(10) = 166 bits. Here BigDecimal is one type you could use instead. But you should have in mind, that 22/7 is just an approximation of pi, and to get it right for 50 digits you need much better formula (e.g. Monte-Carlo method, taylor series, ...). 回答2: You are using a double variable and instead should use