我试图比较两个Double
变量Powershell
。当变量超过 2 位(不计算精度)时,相等性测试意外失败。
我在这里遗漏了一些明显的东西吗?
这是测试脚本输出:
PS C:\test> .\test.ps1
==========
TEST ONE
==========
Value of Double1: 336.1
Type of Double1: System.Double
-----
Value of Double2: 336.2
Type of Double2: System.Double
-----
Value of Double1+.1: 336.2
Type of Double1+.1: System.Double
-----
Does Double1 not equal Double2: True
Does Double1+.1 not equal Double2: True
==========
TEST TWO
==========
Value of Double3: 36.1
Type of Double3: System.Double
-----
Value of Double4: 36.2
Type of Double4: System.Double
-----
Value of Double3+.1: 36.2
Type of Double3+.1: System.Double
-----
Does Double3 not equal Double4: True
Does Double3+.1 not equal Double4: False
这是测试脚本,请注意第一个测试未通过我将 .1 添加到变量的测试,但第二个测试通过。
##################################
# START TEST
##################################
$Double1 = 336.1
$Double2 = 336.2
write-host "=========="
write-host "TEST ONE"
write-host "=========="
write-host "Value of Double1: $Double1"
write-host "Type of Double1:" $Double1.GetType().FullName
write-host "-----"
write-host "Value of Double2: $Double2"
write-host "Type of Double2:" $Double2.GetType().FullName
write-host "-----"
write-host "Value of Double1+.1:" ($Double1+.1)
write-host "-----"
write-host "Does Double1 not equal Double2:" ($Double1 -ne $Double2)
write-host "Does Double1+.1 not equal Double2:" (($Double1+.1) -ne $Double2)
write-host ""
write-host "=========="
write-host "TEST TWO"
write-host "=========="
$Double3 = 36.1
$Double4 = 36.2
write-host ""
write-host "Value of Double3: $Double3"
write-host "Type of Double3:" $Double3.GetType().FullName
write-host "-----"
write-host "Value of Double4: $Double4"
write-host "Type of Double4:" $Double4.GetType().FullName
write-host "-----"
write-host "Value of Double3+.1:" ($Double3+.1)
write-host "-----"
write-host "Does Double3 not equal Double4:" ($Double3 -ne $Double4)
write-host "Does Double3+.1 not equal Double4:" (($Double3+.1) -ne $Double4)
write-host ""