How to properly compare doubles in powershell?


Have noticed the following behaviour when trying to compare two doubles.

Given the following basic script:


If i change the while statement to use less than or equals:
while ($counter -le $maxValue) {


Which is exactly the same as above and thus missing the expected last value of “1.5” at the end.

How can i properly compare two doubles in powershell?


I would suggest not using double to start with. Use System.Decimal (which may have an alias in PowerShell – I’m not sure). It looks like you’re interested in exact decimal values, so use an appropriate type.

The reason you’re not seeing 1.5 is that the closest double to 0.1 is very slightly larger than 0.1… so you’re actually getting numbers like 1.2000000001, 1.3000000002, 1.400000002 – and then 1.500000003 is not less than or equal to 1.5.

See my articles on binary floating point and decimal floating point for more information.


How to properly compare doubles in powershell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply