user4856614
user4856614

Reputation:

Set Python calculating precision higher than 15 digits

for a more precise overview about my problem please look here: Polygonally calculate Pi

I used mpmath, decimal and some other modules to display more than the standard amount of digits, but only the first 15 of them are correct - meaning that when I run my program calculating pi the rest after the first 15 digits is inaccurate.

Or if I say mpf(1/3) it prints 0.3333333333333331574563241647.... Is there any way to let Python calculate with exact numbers? It is very very important for me because my method will produce angles like 0.0000000000000000000000245331°, for example, and if this will be rounded I've got a problem.

Another example I tried is to calculate 0.0001 + 0.0002 - 0.0003 with and without the decimal module. Without it printed 0.0 and with it gave some sort of 1.5223456224E-31. I assume this problem is coming from the binary calculation of Python?

Do you know a language that can calculate it exactly or better how to realise it in Python?

PS: Where does the problem with the lack of precision after 15 digits come from?

Upvotes: 0

Views: 612

Answers (1)

casevh
casevh

Reputation: 11394

The problem is how you create your variables. When you do mpf(1/3), Python first calculates 1/3 as a 64-bit approximation, and then mpmath converts that approximation o a higher precision, but the error is already present. You need to initialize your variables using exact arguments. mpf(1)/3 should provide the expected results.

Upvotes: 0

Related Questions