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:
116.40d-112.375 4,025 # or 116.40-112.375d 4,025
Is this normal ? Expected ?
I came accross this article: http://thepowershellguy.com/blogs/posh/archive/2007/04/02/44-7-integer-of-double.aspx. 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.