I want to calculate pi^2 to 22 decimal places

8 visualizaciones (últimos 30 días)
CanFox
CanFox el 13 de Abr. de 2023
Comentada: CanFox el 14 de Abr. de 2023
I want to calculate pi square to 22 decimal places. MATLAB can do it if we use vpa/digits but these require symbolic math toolbox. Is there any other way in MATLAB without toolbox?
Thanks

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Abr. de 2023
Use the opposite of "long-division". That is, use decimal multiply and add and carry.
Have vectors of decimal digits. Multiply the current "bottom" digit by the current "top" digit; take the final digit of the result and add it to the appropriate position of the result, and if there is a carry digit then add it to the digit to the left of the result (and if the addition results in a value > 9 then reduce by 10 and carry the 1.)
When it is time to move to the next digit on the left in the second value, then move the destination for the first multiplication one position to the left.
Remember that if you need to, expand the working storage to the left.
(For technical reasons it can be easier to work in reverse, storing digits left to right instead of right to left -- makes it easier to expand the destination without having to move anything else in the array.)

Más respuestas (2)

James Tursa
James Tursa el 13 de Abr. de 2023
Editada: James Tursa el 13 de Abr. de 2023
  3 comentarios
Walter Roberson
Walter Roberson el 13 de Abr. de 2023
Though to be fair if the user is permitted to start with a number of digits of π then it might just come down to extended precision multiplication.

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 13 de Abr. de 2023
Editada: John D'Errico el 13 de Abr. de 2023
As a strong suggestion, you want to do some reading here:
Again, you CANNOT do this using doubles, at least not without the effort of writing a higher precision arithmetic tool of your own. I think I used a Chudnovsky series wen I computed pi to all those digits. Remember that series computes the inverse of pi.
But if you are looking for a good approximation that will not even require writing a series, then the wiki page I show has an approximation valid to 30 decimal digits.
I'll write it using the symbolic toolbox, since that is easy here (and I can't easily run HPF in Answers.)
pie = log(sym(640320)^3 + 744)/sqrt(sym(163))
pie = 
vpa(pie,40)
ans = 
3.141592653589793238462643383279726619348
vpa(pi,40)
ans = 
3.141592653589793238462643383279502884197
vpa(pie - pi,40)
ans = 
0.0000000000000000000000000000002237351503804815087144226044020191064753
If you don't want to count how many digits are the same, this will do:
double(ans)
ans = 2.2374e-31
So it is indeed accurate to 30 decimal digits. Again though, you cannot use doubles there. The everything past roughly 16 digitis will be random garbage.
When you are done, and you have computed pi to a sufficient number of digits, multiply it by itself.
And if you really want a series approximation, there is one listed on that page that gives you 8 digits per term of the series. (Thanks to Ramanujan.) So 3 terms will suffice.
(Oh - I checked, and it looks like I computed pi to 500100 decimal digits using HPF.)

Categorías

Más información sobre Numbers and Precision en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by