For some series, such as those shown, you can use an alternating series test to calculate the sum accurate to the desired error. Libraries such as Decimal , GyPy , mpmath or bigfloat , etc., can be used if your calculation affects the accuracy of the built-in floats.
A note on whole approaches :
Although the approximation of the ratio of integers is more accurate, they are completely impractical for real calculations. The reason for this is: 1) adding fractions requires the creation of equal denominators, and this basically requires multiplying the denominators, so by then the size of the numbers is approximately equal to n! (i.e. factorial ); and 2) for an exemplary series, the accuracy of m digits requires m terms. Therefore, even for six-digit accuracy, it is required that the numbers are approximately equal to 1,000,000! = 8 × 10 5,565,708 . For large numbers, this is approximately 10 10 n which quickly becomes completely impractical. Meanwhile, the decimal solution, designed for 6 or 7 or even 40 digits, is trivial.
For example, when solving night strollers, the time and digits of the digits in the denominator or numerator I get:
nt n_digits_in_denominator 10 0.0003 14 100 0.0167 170 1000 5.5027 1727 10000 ???? ???? (gave up after waiting one hour)
And it becomes impractical only for 4 digits of accuracy.
So, if you want to accurately calculate the final and small number of terms and express the final result as a ratio, then whole solutions will be a good choice, but if you want to express the final result as a decimal, you'd better just stick to decimal places.