Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating big number in Powershell

Tags:

powershell

I developed a small script to simulate the Syracuse conjecture in Powershell.

I'm using it with very large number and i'm studying the result in an Excel graph but powershell keeps formatting my number when they are too big :

This is the result i get for the firsts iterations

1.60827282342995E+40 
8.04136411714975E+39 
4.02068205857487E+39 
2.01034102928744E+39 
1.00517051464372E+39 
5.02585257321859E+38 

I would like to have the result without the formatting "E+XX", is there any way to log the entire number to analyse its composition ?

EDIT : The script i wrote :

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

}

}

$val has an arbitrary value

I have Powershell 2.0 only, i'm updating to Powershell v3 right now.

Edit2 : It's very weird now. In the ISE, the script doesn't work, the number is an odd everytime.

I think i managed to find a little solution with the help of bigInt For some reason it doesn't work on ISE but it works on the Powershell v3 cmd.

I modified my LogWrite line for :

LogWrite ([bigint]$chiffre1),$val

or

LogWrite ([bigint]$chiffre2),$val

My log now has the good formatting !

like image 631
0x0000AF56 Avatar asked Jul 30 '16 13:07

0x0000AF56


People also ask

Can PowerShell do math?

You can use one or more arithmetic operators to add, subtract, multiply, and divide values, and to calculate the remainder (modulus) of a division operation. The addition operator ( + ) and multiplication operator ( * ) also operate on strings, arrays, and hashtables.

What is float in PowerShell?

There are two floating-point types that Windows PowerShell uses: the float and the double. The float uses seven digits of precision and the double uses 15–16 digits of precision. The float type is an instance of the System.Single .NET Framework value type, and the double is an instance of the System.Double type.


1 Answers

In PowerShell 3.0+ you can use the System.Numerics.BigInteger data type to represent arbitrarily large integer values:

PS C:\> 1e15
1E+15
PS C:\> 1e15 -as [System.Numerics.BigInteger]
1000000000000000

There's even a builtin type accelerator for it ([bigint]):

PS C:\> [bigint]1e15
1000000000000000
like image 194
Mathias R. Jessen Avatar answered Sep 25 '22 16:09

Mathias R. Jessen