在Powershell中操纵大量数字

0x0*_*F56 3 powershell

我开发了一个小脚本来模拟Powershell中的锡拉库扎猜想。

我使用了大量的数字,并且正在Excel图形中研究结果,但是当它们太大时,powershell会继续格式化我的数字:

这是我第一次迭代得到的结果

1.60827282342995E+40 
8.04136411714975E+39 
4.02068205857487E+39 
2.01034102928744E+39 
1.00517051464372E+39 
5.02585257321859E+38 
Run Code Online (Sandbox Code Playgroud)

我想要不带“ E + XX”格式的结果,是否可以记录整个数字以分析其组成?

编辑:我写的脚本:

Remove-Item "E:\syracuse.txt"

$Logfile = "E:\syracuse.txt"
Function LogWrite
{
Param ([string]$logstring)

Add-content $Logfile -value $logstring
}

$chiffre1 = 0
$chiffre2 = 0


$chiffre1 = read-host "chiffre"

write-host Sequence Initiale $chiffre1

$val = 0

while ($val -ne 32132135464664546546545645656454665412321321231321654657987465432132154)

{


$val++

if ([bool]!($chiffre1%2))

{
   Write-Host "Pair"
   $chiffre2=($chiffre1)/2 
   write-host $chiffre2
   LogWrite $chiffre2,$val

}
Else
{
   Write-Host "Impair"
   $chiffre2 = $chiffre1*3+1
   write-host $chiffre2
   LogWrite $chiffre2,$val

}

if ([bool]!($chiffre2%2))

{
   Write-Host "Pair"
   $chiffre1=($chiffre2)/2 
   write-host $chiffre1
   LogWrite $chiffre1,$val
}
Else
{
   Write-Host "Impair"
   $chiffre1 = $chiffre2*3+1
   write-host $chiffre1
   LogWrite $chiffre1,$val

}

}
Run Code Online (Sandbox Code Playgroud)

$ val具有任意值

我只有Powershell 2.0,我现在要更新到Powershell v3。

Edit2:现在很奇怪。在ISE中,脚本不起作用,每次的数字都是奇数。

我认为我在bigInt的帮助下找到了一些解决方案,由于某种原因,它在ISE上不起作用,但在Powershell v3 cmd上起作用。

我将LogWrite行修改为:

LogWrite ([bigint]$chiffre1),$val
Run Code Online (Sandbox Code Playgroud)

要么

LogWrite ([bigint]$chiffre2),$val
Run Code Online (Sandbox Code Playgroud)

我的日志现在具有良好的格式!

Mat*_*sen 5

在PowerShell 3.0+中,您可以使用System.Numerics.BigInteger数据类型来表示任意大的整数值:

PS C:\> 1e15
1E+15
PS C:\> 1e15 -as [System.Numerics.BigInteger]
1000000000000000
Run Code Online (Sandbox Code Playgroud)

甚至还有一个内置的类型加速器([bigint]):

PS C:\> [bigint]1e15
1000000000000000
Run Code Online (Sandbox Code Playgroud)

  • @ user4317867这与格式无关。默认情况下,PowerShell使用的“ System.Double”类型无法精确存储大量数字。 (4认同)