为什么当答案介于 0 和 1 之间时,PowerShell 有时似乎会给我错误的答案?
PS C:\users\me> 31.2 - 31
0.199999999999999 <<< wrong
PS C:\users\me> 31.2 - 30
1.2 <<< right
PS C:\users\me> 31.2 % 10
1.2 <<< right
PS C:\users\me> 30.2 % 10
0.199999999999999 <<< wrong
PS C:\users\me> 30.2 / 151
0.2 <<< right
最佳答案
您需要使用 [decimal] 来计算街头行人。
[decimal]31.2 - [decimal]31
或
31.2d - 31
数字字面量默认为double,不能表示很多小数(因为需要重复分数,涉及舍入误差等)
引用: https://devcentral.f5.com/articles/powershell-abcs-t-is-for-type-literals
关于PowerShell 数学错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369723/