Can you please tell me how the computer summarizes badly converging rows. He has finite accuracy of calculations, and if we add terms that are much less than the current sum of the series, then accuracy may not be enough. Those. the computer will not be able to add one billionth to the sum of members which it has already counted. And with further summation will produce the same result.
- 3he summarizes them exactly as you teach him. - KoVadim
- With the current price of memory, it is unlikely that there will be a problem when accuracy may not be enough. - Vladimir Gamalyan
- 2You will probably be interested in this question: Calculations on floating-point numbers do not work - Nick Volynkin ♦
2 answers
For "badly converging" series, special calculation algorithms should be used.
The simplest algorithm is the calculation of the sum of blocks of elements and the subsequent summation of the sums of these blocks. The block size is chosen so that when calculating its sum, the loss of accuracy is minimal (for example, starting a new block when the order of the next member decreases compared to the previous one). It works well for monotonous series, but badly for alternating, for which this algorithm is modified, considering separately the blocks of even and odd elements.
It goes without saying that the summation of the block sums is performed from smaller to larger.
If the task is not to lose accuracy, then the appropriate types / libraries should be used. For example, for python, Decimal accuracy is limited only by RAM (by default, 28 characters):
from decimal import Decimal s = Decimal(0) s += Decimal(100000000000000) s += Decimal(0.0000000000001) print (s) 100000000000000.0000000000001
- about the price of memory: acquaintances are engaged in modeling the oceans, there is a multiplication of cubic matrices in the order of 1000x1000x1000. It seems so good memory requires. )) - Nick Volynkin ♦
- What about classes: how does Decimal work inside? Does it store the number as a string? - Nick Volynkin ♦
- @NickVolynkin Well, given the fact that there are fast SSDs, such a matrix is ​​probably not a problem, although I agree, there are tasks where there is a problem in data volumes. At the expense of storage is not aware, the type is built-in, probably depends on the implementation. Although I would not store as a string (too wasteful), maybe something like a long (which takes as many bytes as needed for storing a number) + position with a comma. - Vladimir Gamalyan
- @NickVolynkin, the storage format DECIMAL is described by the IEEE 754 standard. They have three options installed - Decimal32, Decimal64 and Decimal128. If you are interested in details, it is better to read the standard itself - well, there is a lot of text ... - Akina