#need help understanding pi & output of variables

30 messages · Page 1 of 1 (latest)

ocean patrolBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

gilded olive
#
original:
3.141592653589793|2384626433832795028841971693993
output:
3.141592653589793|115997963468544185161590576171875
                 ^ Precision loss after 15 points
gilded olive
#

the first one

#

long double has i think 15 points of precision

#

you may be allowed to set more than 15 decimal points but they will become random after 15 points

barren dew
#

;cpp << std::numeric_limits<long double>::digits10

sullen pewterBOT
#
Program Output
18
barren dew
#

;cpp << std::numeric_limits<double>::digits10

sullen pewterBOT
#
Program Output
15
barren dew
#

technically platform dependent of course (@white verge @gilded olive)

#

For the questions
1: You can specify an arbitrary number of digits in a double literal in the source file, the compiler will convert it to the closest double and then it's converted to the closest long double

#

;compile -std=c++20

#include <iostream>
#include <cmath>
#include <iomanip>
#include <numbers>

int main() {
    double pi_d = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214;
    long double pi_ld1 = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214;
    long double pi_ld2 = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214L; // <--------------- note the L to make it a long double literal
    std::cout << std::setprecision(18);
    std::cout << pi_d << std::endl;
    std::cout << pi_ld1 << std::endl;
    std::cout << pi_ld2 << std::endl;
    std::cout << std::numbers::pi_v<long double> << std::endl;
}
sullen pewterBOT
#
Program Output
3.14159265358979312
3.14159265358979312
3.14159265358979324
3.14159265358979324
barren dew
#

I think 2 should be answered

#

3: pi_v doesn't support 60 decimal places

#

numbers::pi_v<long double> can give you the closest long double representation of pi and setprecision(60) will tell cout to print 60 decimal digits of representation of that number

#

4 how do people / computers calculate pi to X digit... what formula or programing language do they use? (this is kinda dumb also i didnt google this maybe i can find it but incase you know)

There are a lot of cool algorithms 🙂

#

First you'll need some way of actually storing a number that has billions of digits (or whatnot)

#

There are plenty of arbitrary-precision arithmetic libraries out there

#

gmp is a good one

#

for computing, you can do anything from simulating darts being thrown at a board to highly advanced algorithms that I can't claim to understand

#

this arctan formula is cool

#

(there is a better way to approximate the arctan itself too)

#

But, all contemporary records have been set with the chudnovsky formula (or variants of it)

#

it gives you about 14 digits of pi per term

#

hope that helps @white verge

ocean patrolBOT
#

This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.