# Number PI in C?

I'd like to use a more accurate way for implementing the number PI to my C codes. Sadly I couldn't find another way else then using 3.1415... as a double ore float. Does anyone have a method he uses personally ore has time to make a little reserch as I wasn't able to find anything else than importing math.h and again declaring Pi as 3.1415... Greatfull for every answer. Best regards. Here's my small code, just to refer to some example where I'd like to use PI. https://code.sololearn.com/c2TpfDbNT3E4/?ref=app

2/24/2019 11:30:01 PM

Alex Nicolas Kessi7 Answers

New AnswerThe two most common ways I'd go for is in this code: https://code.sololearn.com/cziLwdQ47a0c/#c M_PI is a macro in <math.h> which is defined as the literal 3.14159265358979323846, and acos(-1) also gives an accurate value of PI (at least before you store it into a float. Using either M_PI or acos(-1) directly in your calculations should allow you to yield accurate results or at least approximations. Of course, these can't compare to the PI precision yielded by your code, but either one is 20 times shorter!

`M_PI` or any other mathematical constants are NOT part of C/C++ standard or any other conforming implementation. Also in some implementations such as MS Visual C++, BEFORE including the <math.h> you need to tell the compiler to enable the macro for usage as #define _USE_MATH_DEFINES Also to be safe, before using it, you can make sure that it really exists and if not you must add a conditional compilation block and define the constant there as #ifndef M_PI # define M_PI 3.1415926535897932 #endif _____ MS VC: https://rextester.com/JXQU91866

Alex You can use built in value M_PI, defined in math.h header. printf ("%.16f", M_PI); If you need more precision, then calculate Pi as atan(1)*4

Not to spam but there is a function I found which calculates kinda 50 decimals of PI. I'll leave the question and best answer open if there are any better solutions found to this. Resource: https://crypto.stanford.edu/pbc/notes/pi/code.html Code: https://code.sololearn.com/csUgkfBMG0rk/?ref=app

Fermi I like it 😁 at least a lot easier than my code.. for compiler and coder.

~ swim ~ Fermi Thank you both so hear comes solution 2 might be the best one 😊 https://code.sololearn.com/cZ0y5knddlID/?ref=app