Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing two "Double" variables with -eq/-ne does not validate if number is creater than 2 digits
    primarykey
    data
    text
    <p>Im trying to compare two <code>Double</code> variables in <code>Powershell</code>. When the variable is over 2 digits (not counting precision) the equality test fails unexpectedly. </p> <p>Am I missing something obvious here?</p> <p>Here is the test script output:</p> <pre><code>PS C:\test&gt; .\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 </code></pre> <p>And here is the test script, notice the first test fails the test where I add .1 to the variable, but the second test passes.</p> <pre><code>################################## # 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 "" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload