June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,36 @@
|
|||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Calculate_Pi {
|
||||
private static final MathContext con1024 = new MathContext(1024);
|
||||
private static final BigDecimal bigTwo = new BigDecimal(2);
|
||||
private static final BigDecimal bigFour = new BigDecimal(4);
|
||||
|
||||
private static BigDecimal bigSqrt(BigDecimal bd, MathContext con) {
|
||||
BigDecimal x0 = BigDecimal.ZERO;
|
||||
BigDecimal x1 = BigDecimal.valueOf(Math.sqrt(bd.doubleValue()));
|
||||
while (!Objects.equals(x0, x1)) {
|
||||
x0 = x1;
|
||||
x1 = bd.divide(x0, con).add(x0).divide(bigTwo, con);
|
||||
}
|
||||
return x1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BigDecimal a = BigDecimal.ONE;
|
||||
BigDecimal g = a.divide(bigSqrt(bigTwo, con1024), con1024);
|
||||
BigDecimal t;
|
||||
BigDecimal sum = BigDecimal.ZERO;
|
||||
BigDecimal pow = bigTwo;
|
||||
while (!Objects.equals(a, g)) {
|
||||
t = a.add(g).divide(bigTwo, con1024);
|
||||
g = bigSqrt(a.multiply(g), con1024);
|
||||
a = t;
|
||||
pow = pow.multiply(bigTwo);
|
||||
sum = sum.add(a.multiply(a).subtract(g.multiply(g)).multiply(pow));
|
||||
}
|
||||
BigDecimal pi = bigFour.multiply(a.multiply(a)).divide(BigDecimal.ONE.subtract(sum), con1024);
|
||||
System.out.println(pi);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +1,24 @@
|
|||
function agm_step{T<:FloatingPoint}(x::T, y::T)
|
||||
(0.5*(x + y), sqrt(x*y))
|
||||
end
|
||||
agm1step{T<:AbstractFloat}(x::T, y::T) = (x + y) / 2, sqrt(x * y)
|
||||
|
||||
function approx_pi_step{T<:FloatingPoint,U<:Integer}(x::T, y::T, z::T, n::U)
|
||||
(a, g) = agm_step(x, y)
|
||||
function approxπstep{T<:AbstractFloat}(x::T, y::T, z::T, n::Integer)
|
||||
a, g = agm1step(x, y)
|
||||
k = n + 1
|
||||
s = z + 2^(k+1)*(a^2 - g^2)
|
||||
return (a, g, s, k)
|
||||
s = z + 2 ^ (k + 1) * (a ^ 2 - g ^ 2)
|
||||
return a, g, s, k
|
||||
end
|
||||
|
||||
function approx_pi{T<:FloatingPoint}(a::T, g::T, s::T)
|
||||
4a^2/(1 - s)
|
||||
end
|
||||
approxπ{T<:AbstractFloat}(a::T, g::T, s::T) = 4a ^ 2 / (1 - s)
|
||||
|
||||
prec = 512
|
||||
set_bigfloat_precision(prec)
|
||||
println("Approximating pi using ", prec, "-bit floats.")
|
||||
setprecision(512)
|
||||
a, g, s, k = big(1.0), 1 / √big(2.0), big(0.0), 0
|
||||
oldπ = big(0.0)
|
||||
println("Approximating π using ", precision(BigFloat), "-bit floats.")
|
||||
println(" k Error Result")
|
||||
a = big(1.0)
|
||||
g = a/sqrt(big(2.0))
|
||||
s = big(0.0)
|
||||
k = 0
|
||||
oldagpi = big(0.0)
|
||||
for i in 1:100
|
||||
(a, g, s, k) = approx_pi_step(a, g, s, k)
|
||||
agpi = approx_pi(a, g, s)
|
||||
2eps(agpi) < abs(agpi-oldagpi) || break
|
||||
oldagpi = agpi
|
||||
err = pi - agpi
|
||||
print(@sprintf(" %2d ", i))
|
||||
print(@sprintf(" %9.1e", err))
|
||||
println(@sprintf(" %.60e", agpi))
|
||||
a, g, s, k = approxπstep(a, g, s, k)
|
||||
estπ = approxπ(a, g, s)
|
||||
if abs(estπ - oldπ) < 2eps(estπ) break end
|
||||
oldπ = estπ
|
||||
err = abs(π - estπ)
|
||||
@printf("%4d%10.1e%68.60e\n", i, err, estπ)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
import java.math.MathContext
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.compat.Platform.currentTime
|
||||
import scala.math.BigDecimal
|
||||
|
||||
object Calculate_Pi extends App {
|
||||
val precision = new MathContext(32768 /*65536*/)
|
||||
val (bigZero, bigOne, bigTwo, bigFour) =
|
||||
(BigDecimal(0, precision), BigDecimal(1, precision), BigDecimal(2, precision), BigDecimal(4, precision))
|
||||
|
||||
def bigSqrt(bd: BigDecimal) = {
|
||||
@tailrec
|
||||
def iter(x0: BigDecimal, x1: BigDecimal): BigDecimal =
|
||||
if (x0 == x1) x1 else iter(x1, (bd / x1 + x1) / bigTwo)
|
||||
|
||||
iter(bigZero, BigDecimal(Math.sqrt(bd.toDouble), precision))
|
||||
}
|
||||
|
||||
@tailrec
|
||||
private def loop(a: BigDecimal, g: BigDecimal, sum: BigDecimal, pow: BigDecimal): BigDecimal = {
|
||||
if (a == g) (bigFour * (a * a)) / (bigOne - sum)
|
||||
else {
|
||||
val (_a, _g, _pow) = ((a + g) / bigTwo, bigSqrt(a * g), pow * bigTwo)
|
||||
loop(_a, _g, sum + ((_a * _a - (_g * _g)) * _pow), _pow)
|
||||
}
|
||||
}
|
||||
|
||||
println(precision)
|
||||
val pi = loop(bigOne, bigOne / bigSqrt(bigTwo), bigZero, bigTwo)
|
||||
println(s"This are ${pi.toString.length - 1} digits of π:")
|
||||
val lines = pi.toString().sliding(103, 103).mkString("\n")
|
||||
println(lines)
|
||||
|
||||
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue