pi

How to get decimal to n places precision in C# program for Pi

自闭症网瘾萝莉.ら 提交于 2019-12-23 17:29:37
问题 With reg to this question Pi in C# I coded the below code and gives a output with last 6 digits as 0. So I want to improve the program by converting everything to decimal. I have never used decimal in C# instead of a double before and I am only comfortable with double in my regular use. So please help me with decimal conversion, i tried to replace all double to decimal at start and it didnt turn good :(. using System; class Program { static void Main() { Console.WriteLine(" Get PI from

Non-Terminating Decimal Error Even With MathContext

ぃ、小莉子 提交于 2019-12-23 09:46:54
问题 I'm crafting code to implement this algorithm: However, I'm getting this error, even with MathContext(1000): Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(BigDecimal.java:1603) at picalculator.PiCalculator.calculatePi(PiCalculator.java:59) at picalculator.PiCalculator.main(PiCalculator.java:25) Java Result: 1 While using this method: public static void calculatePi() {

Non-Terminating Decimal Error Even With MathContext

自闭症网瘾萝莉.ら 提交于 2019-12-23 09:46:47
问题 I'm crafting code to implement this algorithm: However, I'm getting this error, even with MathContext(1000): Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(BigDecimal.java:1603) at picalculator.PiCalculator.calculatePi(PiCalculator.java:59) at picalculator.PiCalculator.main(PiCalculator.java:25) Java Result: 1 While using this method: public static void calculatePi() {

Cannot draw pixels, Pi number in a Synesthetic way

老子叫甜甜 提交于 2019-12-22 11:03:03
问题 I want to print each digit of pi number as a colored pixel, so, I get an input, with the pi number, then parse it into a list, each node containing a digit (I know, I'll use an array later), but I never get this painted to screen... Can someone help me to see where I'm wrong? import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.image.MemoryImageSource; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util

Cython's calculations are incorrect

旧街凉风 提交于 2019-12-21 12:07:33
问题 I implemented the Madhava–Leibniz series to calculate pi in Python, and then in Cython to improve the speed. The Python version: from __future__ import division pi = 0 l = 1 x = True while True: if x: pi += 4/l else: pi -= 4/l x = not x l += 2 print str(pi) The Cython version: cdef float pi = 0.0 cdef float l = 1.0 cdef unsigned short x = True while True: if x: pi += 4.0/l else: pi -= 4.0/l x = not x l += 2 print str(pi) When I stopped the Python version it had correctly calculated pi to 3

can't print more decimal of pi [duplicate]

雨燕双飞 提交于 2019-12-20 04:13:19
问题 This question already has answers here : How do I print a double value with full precision using cout? (10 answers) Closed 4 years ago . I have tried to use long double type in my program to print out more digits of pi. But it only shows 5 digits decimal. Here is my code. int main(int argc, char** argv) { long double pi_18 = acos(static_cast<long double>(-1)); cout << "pi to 18:" << pi_18 << endl; return 0; } and this is my output: pi to 18: 3.14159 How can I fix this problem? 回答1: Like so:

Importing Math.PI as reference or value

我是研究僧i 提交于 2019-12-19 10:08:50
问题 I'm preparing for a basic certification in Java. I'm a little confused by the answer to a question that I have got right(!):- Given: public class Circle { static double getCircumference(double radius ) { return PI * 2 * radius; } public static double getArea(double radius) { return PI * radius * radius; } } Which import statement will enable the code to compile and run? import java.lang.*; import static java.lang.Math.PI; import java.lang.Math.*; import java.lang.Math; I answered import

Fast algorithm to calculate Pi in parallel

隐身守侯 提交于 2019-12-18 11:45:59
问题 I am starting to learn CUDA and I think calculating long digits of pi would be a nice, introductory project. I have already implemented the simple Monte Carlo method which is easily parallelize-able. I simply have each thread randomly generate points on the unit square, figure out how many lie within the unit circle, and tally up the results using a reduction operation. But that is certainly not the fastest algorithm for calculating the constant. Before, when I did this exercise on a single

Why do sin(45) and cos(45) give different results? [duplicate]

不问归期 提交于 2019-12-18 07:12:29
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 4 years ago . This is something I wasn't expecting. I know these numbers are not 100% exact, but I wasn't expecting complementary angles giving different results of sin and cos : This following function returns 0.70710678118654746000000... sin(45 * PI / 180.0); while this follwing function returns 0.70710678118654757000000... cos(45 * PI / 180.0); so, it's: 0.707106781186547**46**000000... vs 0

Why do sin(45) and cos(45) give different results? [duplicate]

元气小坏坏 提交于 2019-12-18 07:12:26
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 4 years ago . This is something I wasn't expecting. I know these numbers are not 100% exact, but I wasn't expecting complementary angles giving different results of sin and cos : This following function returns 0.70710678118654746000000... sin(45 * PI / 180.0); while this follwing function returns 0.70710678118654757000000... cos(45 * PI / 180.0); so, it's: 0.707106781186547**46**000000... vs 0