PoshCode Logo PowerShell Code Repository

TheMathFunction by Joel Bennett 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2094"></script>download | new post

You need to download and unpack LoreSoft.MathExpressions.dll into your Documents\WindowsPowerShell\Libraries or tweak this module.

Examples:

C:\PS> math 3 ^ sqrt pi
7.00931221713741

C:\PS> math sin sqrt 3
0.987026644990354

C:\PS> math ‘(42 / 9) * ( cos(.56) ^ 3 ) + Tan 60’
3.15828206698

C:\PS> (math (2mb) ^ 2)/1gb
4096

  1. Add-Type -Path (Join-Path (Split-Path $Profile) Libraries\LoreSoft.MathExpressions.dll)
  2. ## You can dot-source this in 1.0 after uncommenting the following line, and deleting the first and last lines.
  3. # [Reflection.Assembly]::LoadFrom((Join-Path (Split-Path $Profile) Libraries\LoreSoft.MathExpressions.dll)) | Out-Null
  4.  
  5. $MathEvaluator = New-Object LoreSoft.MathExpressions.MathEvaluator
  6.  
  7. Function Use-Math {
  8.    $MathEvaluator.Evaluate( ($args -join " ") )
  9. }
  10.  
  11. Set-Alias Math Use-Math
  12.  
  13. Export-ModuleMember -Function * -Alias *

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me