【问题标题】:Windows Powershell equivalent to this Linux command that calculates a number to 10000 decimal places?Windows Powershell 相当于这个 Linux 命令,它计算一个小数点后 10000 位的数字?
【发布时间】:2021-03-23 10:54:52
【问题描述】:

我在社交媒体上看到了这个有趣的小命令,它将 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 中没有用于此的本机功能。 .NET 没有内置任意精度的数值库,因此 PowerShell 也没有。对于整数计算,有BigInteger,但不能用于像这样的浮点计算。您可以为 .NET 查找任意精度的数值库并在 PowerShell 中使用这些库,或者只安装并调用 bc(它是一个单独的可执行文件,而不是内置命令)。
  • 有一个DECIMAL type in .NET,但这仅对96位精度有用,而且您似乎无法在其上使用[Math]::三角函数。
  • 当然,只要您不介意作弊,原生等效项可以是(Invoke-WebRequest https://www.pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt).Content | Select -First 10001。你可以请求更多的数字,它比bc 快得多。 :-P

标签: powershell math decimal precision


【解决方案1】:

[Math] class 只适用于double,所以很明显你不能获得超过 53 位的精度。它的一些方法还支持decimal,所以你会得到更多的数字,但显然不可能接近 10000 位。 .NET 中没有 arbitrary precision 浮点类型,因此如果您不想从某些存储空间中检索数字,您完全可以自己计算数字

不过,有 BigInteger(PowerShell 类型加速器名称:[bigint]),这很有帮助,因为您不需要自己进行任意精度的数学运算。例如要计算 π 的 10000 位,您可以计算 10000π 并以整数或 fixed point math

进行运算

有many algorithms to do that比如这个:How does this code calculate pi with high precision?

幸运的是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 = ""
        }
    }

【讨论】:

  • 简单本身!这只是 PowerShell 作为数字运算语言全面就业的第一步。 :-)
猜你喜欢
  • 2019-07-21
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
相关资源
最近更新 更多