Windows Powershell 等效于此 Linux 命令,可将数字计算到小数点后 10000 位?
Windows Powershell equivalent to this Linux command that calculates a number to 10000 decimal places?
我在社交媒体上看到过这个有趣的小命令,它输出 pi 到小数点后 10000 位。我想知道与此等效的 Windows Powershell 是什么?
echo "scale=10000; 4*a(1)" | bc -l
我知道我可以使用 Powershell Math library 来做基本的数学公式,但是如何将小数位数设置为 10000?
PS C:\Users\Me> [Math]::Atan(1)*4
3.14159265358979
.NET 中的 [Math]
class only works with double
so obviously you can't get more than 53 bits of precision. A few of its methods also support decimal so you'll get a little bit more digits but obviously no way near 10000 digits. There's no arbitrary precision 浮点类型,因此如果您不想从某些存储中检索数字,您完全可以自己计算数字 space
有BigInteger (PowerShell type accelerator name: [bigint]
) though, which helps a lot since you won't need to do arbitrary precision math yourself. For example to calculate 10000 digits of π you can calculate 10000π and does the operations in integer or fixed point math
有many algorithms to do that such as this one:
还好Rosetta Code has a sample snippet to calculate π。声明以下函数后,只需调用 Get-Pi 10000
即可获得所需的输出
Function Get-Pi ( $Digits )
{
$Big = [bigint[]](0..10)
$ndigits = 0
$Output = ""
$q = $t = $k = $Big[1]
$r = $Big[0]
$l = $n = $Big[3]
# Calculate first digit
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
$Output += [string]$n + '.'
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
While ( $ndigits -lt $Digits )
{
While ( $ndigits % 100 -ne 0 -or -not $Output )
{
If ( $Big[4] * $q + $r - $t -lt $n * $t )
{
$Output += [string]$n
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
}
Else
{
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
}
}
$Output
$Output = ""
}
}
我在社交媒体上看到过这个有趣的小命令,它输出 pi 到小数点后 10000 位。我想知道与此等效的 Windows Powershell 是什么?
echo "scale=10000; 4*a(1)" | bc -l
我知道我可以使用 Powershell Math library 来做基本的数学公式,但是如何将小数位数设置为 10000?
PS C:\Users\Me> [Math]::Atan(1)*4
3.14159265358979
.NET 中的 [Math]
class only works with double
so obviously you can't get more than 53 bits of precision. A few of its methods also support decimal so you'll get a little bit more digits but obviously no way near 10000 digits. There's no arbitrary precision 浮点类型,因此如果您不想从某些存储中检索数字,您完全可以自己计算数字 space
有BigInteger (PowerShell type accelerator name: [bigint]
) though, which helps a lot since you won't need to do arbitrary precision math yourself. For example to calculate 10000 digits of π you can calculate 10000π and does the operations in integer or fixed point math
有many algorithms to do that such as this one:
还好Rosetta Code has a sample snippet to calculate π。声明以下函数后,只需调用 Get-Pi 10000
即可获得所需的输出
Function Get-Pi ( $Digits )
{
$Big = [bigint[]](0..10)
$ndigits = 0
$Output = ""
$q = $t = $k = $Big[1]
$r = $Big[0]
$l = $n = $Big[3]
# Calculate first digit
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
$Output += [string]$n + '.'
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
While ( $ndigits -lt $Digits )
{
While ( $ndigits % 100 -ne 0 -or -not $Output )
{
If ( $Big[4] * $q + $r - $t -lt $n * $t )
{
$Output += [string]$n
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
}
Else
{
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
}
}
$Output
$Output = ""
}
}