Pi: Difference between revisions
Content added Content deleted
m (Eliminated extra space before the final digit that is output) |
|||
Line 3,881: | Line 3,881: | ||
WriteString[$Output, RealDigits[Pi, 10, 1, i][[1, 1]]]; Pause[.05]];</syntaxhighlight> |
WriteString[$Output, RealDigits[Pi, 10, 1, i][[1, 1]]]; Pause[.05]];</syntaxhighlight> |
||
=={{header|MATLAB |
=={{header|MATLAB}}== |
||
Requires the Variable Precision Integer (vpi) Toolbox |
|||
Matlab and Octave use double precision numbers per default, and pi is a builtin constant value. Arbitrary precision is only implemented in some additional toolboxes (e.g. symbolic toolbox). |
|||
<syntaxhighlight lang="matlab" |
<syntaxhighlight lang="matlab"> |
||
function pi_str = piSpigot(N) |
|||
% Return N digits of pi using Gibbons's first spigot algorithm. |
|||
% If N is omitted, the digits are printed ad infinitum. |
|||
% Uses the expansion |
|||
% pi = sum_{i=0} (i!)^2 2^{i+1} /(2i+1)! |
|||
% = 2 + 1/3 * ( 2 + 2/5 * (2 + 3/7 * ( 2 + 4/9 * ( ..... ))))) |
|||
% = (2 + 1/3 *)(2 + 2/5 *)(2 + 3/7 *)... |
|||
% where the terms in the last expression represent Linear Fractional |
|||
% Transforms (LFTs). |
|||
% |
|||
% Requires the Variable Precision Integer (vpi) Toolbox |
|||
% |
|||
% Reference: |
|||
% "Unbounded Spigot Algorithms for the Digits of Pi" by J. Gibbons, 2004 |
|||
% American Mathematical Monthly, vol. 113. |
|||
if nargin < 1 |
|||
N = Inf; |
|||
lineLength = 50; |
|||
else |
|||
pi_str = repmat(' ',1,N); |
|||
end |
|||
q = vpi(1); |
|||
r = vpi(0); |
|||
t = vpi(1); |
|||
k = 1; % If printing more than 3E15 digits, use k = vpi(1); |
|||
i = 1; |
|||
first_digit = true; |
|||
while i <= N |
|||
threeQplusR = 3*q + r; |
|||
n = double(threeQplusR / t); |
|||
if q+threeQplusR < (n+1)*t |
|||
d = num2str(n); |
|||
if isinf(N) |
|||
fprintf(1,'%s', d); |
|||
if first_digit |
|||
fprintf(1,'.'); |
|||
first_digit = false; |
|||
i = i+1; |
|||
end |
|||
if i == lineLength |
|||
fprintf(1,'\n'); |
|||
i = 0; |
|||
end |
|||
else |
|||
pi_str(i) = d; |
|||
end |
|||
q = 10*q; |
|||
r = 10*(r-n*t); |
|||
i = i + 1; |
|||
else |
|||
t = (2*k+1)*t; |
|||
r = (4*k+2)*q + (2*k+1)*r; |
|||
q = k*q; |
|||
k = k + 1; |
|||
end |
|||
end |
|||
end |
|||
</syntaxhighlight> |
|||
<pre> |
<pre> |
||
>> |
>> piSpigot |
||
3.141592653589793238462643383279502884197169399375 |
|||
ans = 3.1416 |
|||
10582097494459230781640628620899862803482534211706 |
|||
> printf('%.60f\n',pi) |
|||
79821480865132823066470938446095505822317253594081 |
|||
3.141592653589793115997963468544185161590576171875000000000000>> format long |
|||
28481117450284102701938521105559644622948954930381 |
|||
96442881097566593344612847564823378678316527120190 |
|||
91456485669234603486104543266482133936072602491412 |
|||
</pre> |
</pre> |
||
<pre> |
|||
Unfortunately this is not the correct value! |
|||
3.14159265358979323846264338327950288419716939937510582 |
|||
=================??????????????????????????????????????</pre> |
|||
Calling for 60 digit output does not produce 60 digits of precision. Once the sixteen digit precision of double precision is reached, the subsequent digits are determined by the workings of the binary to decimal conversion. The long decimal string is the exact decimal value of the binary representation of pi, which binary value is itself not exact because pi cannot be represented in a finite number of digits, be they decimal, binary or any other integer base... |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |