Windows Vista Forums

Operator -not should get True for whatever -eq 0 gets True (and morefun)

  1. #1


    Roman Kuzmin Guest

    Operator -not should get True for whatever -eq 0 gets True (and morefun)

    Do I miss something (see the code and comments below)? I am with V2
    CTP3, do other versions work in the same way?

    # Operator -not should get True for whatever -eq 0 gets True

    $ToBe = 0

    $ToBe -eq 0 # True, OK
    -not $ToBe # True, OK

    $ToBe = (Get-Variable ToBe).Options

    $ToBe -eq 0 # True, OK
    -not $ToBe # False, WOW, leads to subtle bugs, difficult to find

    <#

    Actual output:
    True
    True
    True
    False

    Expected output:
    True
    True
    True
    True

    #>

    # How do you like this: both conditions are true
    if ($ToBe -eq 0) {
    "This is true, OK"
    }
    if ($ToBe) {
    "And this is true, WOW"
    }

    <#

    Output:
    This is true, OK
    And this is true, WOW

    #>

      My System SpecsSystem Spec

  2.   


  3. #2


    Vadims Podans Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (and more fun)

    PowerShell 1.0:

    [user name] $tobe = 0
    [user name] if ($ToBe -eq 0) {

    >> "This is true, OK"
    >> }
    >> if ($ToBe) {
    >> "And this is true, WOW"
    >> }
    >>
    This is true, OK
    [user name]

    about your first example:
    check the type of
    ((Get-Variable ToBe).Options0.gettype().fullname
    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv


    "Roman Kuzmin" <nightroman@xxxxxx> rakstija zinojuma
    "news:dd554f76-1e88-4366-8bca-2176d2d9273f@xxxxxx"...

    > Do I miss something (see the code and comments below)? I am with V2
    > CTP3, do other versions work in the same way?
    >
    > # Operator -not should get True for whatever -eq 0 gets True
    >
    > $ToBe = 0
    >
    > $ToBe -eq 0 # True, OK
    > -not $ToBe # True, OK
    >
    > $ToBe = (Get-Variable ToBe).Options
    >
    > $ToBe -eq 0 # True, OK
    > -not $ToBe # False, WOW, leads to subtle bugs, difficult to find
    >
    > <#
    >
    > Actual output:
    > True
    > True
    > True
    > False
    >
    > Expected output:
    > True
    > True
    > True
    > True
    >
    > #>
    >
    > # How do you like this: both conditions are true
    > if ($ToBe -eq 0) {
    > "This is true, OK"
    > }
    > if ($ToBe) {
    > "And this is true, WOW"
    > }
    >
    > <#
    >
    > Output:
    > This is true, OK
    > And this is true, WOW
    >
    > #>

      My System SpecsSystem Spec

  4. #3


    Roman Kuzmin Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (andmore fun)

    Hi Vadims,

    So, do I understand correctly that in V1 it works differently? Namely,
    "And this is true, WOW" is not written?

    As for this
    0> (Get-Variable ToBe).Options.GetType() | Out-String

    ... the type is enum:

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True ScopedItemOptions System.Enum

    And what is the type there in V1?

      My System SpecsSystem Spec

  5. #4


    Roman Kuzmin Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (andmore fun)

    Hmm, I guess your example is for int 0, not enum 0. Try this:

    $Foo = 0
    $ToBe = (Get-Variable Foo).Options
    if ($ToBe -eq 0) {
    "This is true, OK"
    }
    if ($ToBe) {
    "And this is true, WOW"
    }

    <#
    Output:
    This is true, OK
    And this is true, WOW
    #>


      My System SpecsSystem Spec

  6. #5


    Vadims Podans Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (and more fun)

    [user name] $tobe
    None
    [user name] $tobe -eq 0
    True

    therefore if you check this:
    $ToBe = (Get-Variable Foo).Options
    if ($ToBe -eq 0) {
    "This is true, OK"
    }

    you'll get $true. If you check like this:
    if ($ToBe) {
    "And this is true, WOW"
    }

    you get $true too, because if ($tobe) is the same as "if ($tobe -ne
    $null -or $tobe -ne $false)". But your variable is not $null, or $false:
    [user name] $tobe -eq $null
    False
    [user name] $tobe -eq $true
    False
    [user name] $tobe -eq $false
    False

    so, I sure that it is normal.

    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "Roman Kuzmin" <nightroman@xxxxxx> rakstija zinojuma
    "news:11173878-088f-4acd-b2cb-07b4c61e28df@xxxxxx"...

    > Hmm, I guess your example is for int 0, not enum 0. Try this:
    >
    > $Foo = 0
    > $ToBe = (Get-Variable Foo).Options
    > if ($ToBe -eq 0) {
    > "This is true, OK"
    > }
    > if ($ToBe) {
    > "And this is true, WOW"
    > }
    >
    > <#
    > Output:
    > This is true, OK
    > And this is true, WOW
    > #>
    >

      My System SpecsSystem Spec

  7. #6


    Vadims Podans Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (and more fun)

    if ($tobe) is the same as "if ($tobe -ne $null -or $tobe -ne $false)"

    here must be -and instead -or:
    if ($tobe -ne $null -AND $tobe -ne $false)

    Sorry.

    "Vadims Podans" <vpodans> rakstija zinojuma
    "news:OkHrP#9dJHA.4412@xxxxxx"...

    > [user name] $tobe
    > None
    > [user name] $tobe -eq 0
    > True
    >
    > therefore if you check this:
    > $ToBe = (Get-Variable Foo).Options
    > if ($ToBe -eq 0) {
    > "This is true, OK"
    > }
    >
    > you'll get $true. If you check like this:
    > if ($ToBe) {
    > "And this is true, WOW"
    > }
    >
    > you get $true too, because if ($tobe) is the same as "if ($tobe -ne
    > $null -or $tobe -ne $false)". But your variable is not $null, or $false:
    > [user name] $tobe -eq $null
    > False
    > [user name] $tobe -eq $true
    > False
    > [user name] $tobe -eq $false
    > False
    >
    > so, I sure that it is normal.
    >
    > --
    > WBR, Vadims Podans
    > PowerShell blog - www.sysadmins.lv
    >
    > "Roman Kuzmin" <nightroman@xxxxxx> rakstija zinojuma
    > "news:11173878-088f-4acd-b2cb-07b4c61e28df@xxxxxx"...

    >> Hmm, I guess your example is for int 0, not enum 0. Try this:
    >>
    >> $Foo = 0
    >> $ToBe = (Get-Variable Foo).Options
    >> if ($ToBe -eq 0) {
    >> "This is true, OK"
    >> }
    >> if ($ToBe) {
    >> "And this is true, WOW"
    >> }
    >>
    >> <#
    >> Output:
    >> This is true, OK
    >> And this is true, WOW
    >> #>
    >>

      My System SpecsSystem Spec

  8. #7


    Roman Kuzmin Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (andmore fun)

    I think that by this

    if ($ToBe) {

    we ask PowerShell to convert $ToBe to Boolean. It is in fact enum with
    value 0. It does not look OK to me that on conversion to Boolean it is
    at first converted to something that has presentation "None". Or, if
    this is still OK, than why on conversion to int ($ToBe -eq 0) we get
    true, as if "None" is the same as 0. Where is logic? Why "None" is 0,
    but "None" is not False?

      My System SpecsSystem Spec

  9. #8


    Vadims Podans Guest

    Re: Operator -not should get True for whatever -eq 0 gets True (and more fun)

    no. Here is a trick, because "none" in fact is not necessarily digit 0. Look
    here:
    example with string:
    [user name] $a = [string]0
    [user name] $a -eq $true
    False
    [user name] $a -eq $false
    False
    [user name] $a -eq 0
    True
    [user name] if ($a){"yes"} else {"something wrong"}
    yes

    example with digit:
    [user name] $a = [int]0
    [user name] $a -eq $true
    False
    [user name] $a -eq $false
    True
    [user name] $a -eq 0
    True
    [user name] if ($a){"yes"} else {"something wrong"}
    something wrong
    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "Roman Kuzmin" <nightroman@xxxxxx> rakstija zinojuma
    "news:68adc363-c494-497f-80a2-40f8484a5682@xxxxxx"...

    > I think that by this
    >
    > if ($ToBe) {
    >
    > we ask PowerShell to convert $ToBe to Boolean. It is in fact enum with
    > value 0. It does not look OK to me that on conversion to Boolean it is
    > at first converted to something that has presentation "None". Or, if
    > this is still OK, than why on conversion to int ($ToBe -eq 0) we get
    > true, as if "None" is the same as 0. Where is logic? Why "None" is 0,
    > but "None" is not False?

      My System SpecsSystem Spec


Operator -not should get True for whatever -eq 0 gets True (and morefun)
Similar Threads
Thread Forum
3D-Analyze, true or not true? Chillout Room
is this true Vista General
Is this true? Vista General
Why is $true the same as 'TRUE' PowerShell
True number of true Vista users Vista General