Doing some maths with powershell…

I’ve been doing sometimes some mathematics in the powershell console and got surprised by the result when I typed:


Then I did


So to obtain the correct result, I had to change one of the [double] to a [decimal] type:

# or

Is this normal ? Expected ?

I came accross this article: But there isn’t a clear explanation of what’s going on.

I finally found the answer is Bruce Payette book’s Powershell In Action

The tendency is to characterize PowerShell as a dynamically typed language, but a better description is that PowerShell is a type-promiscuous language (sounds salacious doesn’t it?). By type-promiscuous, we mean that PowerShell will expend a tremendous amount of effort trying to turn what you have into what you need with as little work on your part as it can manage.
In practice, the interpreter is very careful about making sure its transformations are reasonable and that no information is unexpectedly lost. This is particularly important when dealing with numeric calculations. In PowerShell, you can freely mix and match different types of numbers in expressions. You can even include strings in this mix. PowerShell converts everything as needed as long as there is no loss in precision without specific guidance from the user.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s