Python: bool(-1) is returning true. Why does this happen?










0















I decided to cast -1 into a bool to check what the outcome was.



Contrary to my Expectation that bool(-1) would return FALSE it returns TRUE.



Why is this the case?










share|improve this question



















  • 1





    Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

    – Some programmer dude
    Nov 14 '18 at 8:22











  • Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

    – usr2564301
    Nov 14 '18 at 9:31
















0















I decided to cast -1 into a bool to check what the outcome was.



Contrary to my Expectation that bool(-1) would return FALSE it returns TRUE.



Why is this the case?










share|improve this question



















  • 1





    Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

    – Some programmer dude
    Nov 14 '18 at 8:22











  • Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

    – usr2564301
    Nov 14 '18 at 9:31














0












0








0








I decided to cast -1 into a bool to check what the outcome was.



Contrary to my Expectation that bool(-1) would return FALSE it returns TRUE.



Why is this the case?










share|improve this question
















I decided to cast -1 into a bool to check what the outcome was.



Contrary to my Expectation that bool(-1) would return FALSE it returns TRUE.



Why is this the case?







python boolean






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:18









D.Mendes

11810




11810










asked Nov 14 '18 at 8:21









VenkatVenkat

342




342







  • 1





    Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

    – Some programmer dude
    Nov 14 '18 at 8:22











  • Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

    – usr2564301
    Nov 14 '18 at 9:31













  • 1





    Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

    – Some programmer dude
    Nov 14 '18 at 8:22











  • Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

    – usr2564301
    Nov 14 '18 at 9:31








1




1





Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

– Some programmer dude
Nov 14 '18 at 8:22





Generally (for many programming languages) only zero is considered false. Everything non-zero is true.

– Some programmer dude
Nov 14 '18 at 8:22













Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

– usr2564301
Nov 14 '18 at 9:31






Generally (for many programming languages) there is detailed information to be found online, so you don't have to check and see what happens. See docs.python.org/3.7/library/functions.html#bool of the official documentation.

– usr2564301
Nov 14 '18 at 9:31













2 Answers
2






active

oldest

votes


















2














Based on Python 3 documentation, most of the built-in objects considered false are:




  • Constants defined to be false: None and False.

  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

  • Empty sequences and collections: '', (), , , set(), range(0)



Notice that it only specifies zero to be false, instead of any value that is equal to or lower than 0. This is also common in various other programming languages.






share|improve this answer




















  • 1





    ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

    – NPE
    Nov 14 '18 at 9:22


















0














TL;DR



Yes, this is expected. Most programming languages only consider 0 to be false.



Elaborated



This makes sense when you consider how -1 is represented in the processor. Typically you convert a positive number to it's negative counterpart by using 2's complement. 2's complent represents a negative binary number by inverting all the bits and adding 1. Say we have a 4-bit architecture, the decimal 1 is represented as 0001. To get its negative representation, invert all the bits and add 1, thus 0001 becomes 1110 + 1 = 1111.



When we want to know if a value is false, we can or all its inputs, if the result is 0, it's false, otherwise it's true, and thus it makes perfect sense that -1 == true.



The reson that we use 2's complement, as appose to 1's complement (where we just invert all bits without adding 1) is exacly because we do not want to be able to represent both 0 and -0.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295735%2fpython-bool-1-is-returning-true-why-does-this-happen%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Based on Python 3 documentation, most of the built-in objects considered false are:




    • Constants defined to be false: None and False.

    • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

    • Empty sequences and collections: '', (), , , set(), range(0)



    Notice that it only specifies zero to be false, instead of any value that is equal to or lower than 0. This is also common in various other programming languages.






    share|improve this answer




















    • 1





      ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

      – NPE
      Nov 14 '18 at 9:22















    2














    Based on Python 3 documentation, most of the built-in objects considered false are:




    • Constants defined to be false: None and False.

    • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

    • Empty sequences and collections: '', (), , , set(), range(0)



    Notice that it only specifies zero to be false, instead of any value that is equal to or lower than 0. This is also common in various other programming languages.






    share|improve this answer




















    • 1





      ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

      – NPE
      Nov 14 '18 at 9:22













    2












    2








    2







    Based on Python 3 documentation, most of the built-in objects considered false are:




    • Constants defined to be false: None and False.

    • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

    • Empty sequences and collections: '', (), , , set(), range(0)



    Notice that it only specifies zero to be false, instead of any value that is equal to or lower than 0. This is also common in various other programming languages.






    share|improve this answer















    Based on Python 3 documentation, most of the built-in objects considered false are:




    • Constants defined to be false: None and False.

    • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

    • Empty sequences and collections: '', (), , , set(), range(0)



    Notice that it only specifies zero to be false, instead of any value that is equal to or lower than 0. This is also common in various other programming languages.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 9:24









    jpp

    100k2161111




    100k2161111










    answered Nov 14 '18 at 8:36









    AndreasAndreas

    1,91131018




    1,91131018







    • 1





      ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

      – NPE
      Nov 14 '18 at 9:22












    • 1





      ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

      – NPE
      Nov 14 '18 at 9:22







    1




    1





    ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

    – NPE
    Nov 14 '18 at 9:22





    ...and to make the answer complete, I'd also mention that bool() uses the standard truth testing protocol that you quote from. docs.python.org/3/library/stdtypes.html#boolean-values

    – NPE
    Nov 14 '18 at 9:22













    0














    TL;DR



    Yes, this is expected. Most programming languages only consider 0 to be false.



    Elaborated



    This makes sense when you consider how -1 is represented in the processor. Typically you convert a positive number to it's negative counterpart by using 2's complement. 2's complent represents a negative binary number by inverting all the bits and adding 1. Say we have a 4-bit architecture, the decimal 1 is represented as 0001. To get its negative representation, invert all the bits and add 1, thus 0001 becomes 1110 + 1 = 1111.



    When we want to know if a value is false, we can or all its inputs, if the result is 0, it's false, otherwise it's true, and thus it makes perfect sense that -1 == true.



    The reson that we use 2's complement, as appose to 1's complement (where we just invert all bits without adding 1) is exacly because we do not want to be able to represent both 0 and -0.






    share|improve this answer



























      0














      TL;DR



      Yes, this is expected. Most programming languages only consider 0 to be false.



      Elaborated



      This makes sense when you consider how -1 is represented in the processor. Typically you convert a positive number to it's negative counterpart by using 2's complement. 2's complent represents a negative binary number by inverting all the bits and adding 1. Say we have a 4-bit architecture, the decimal 1 is represented as 0001. To get its negative representation, invert all the bits and add 1, thus 0001 becomes 1110 + 1 = 1111.



      When we want to know if a value is false, we can or all its inputs, if the result is 0, it's false, otherwise it's true, and thus it makes perfect sense that -1 == true.



      The reson that we use 2's complement, as appose to 1's complement (where we just invert all bits without adding 1) is exacly because we do not want to be able to represent both 0 and -0.






      share|improve this answer

























        0












        0








        0







        TL;DR



        Yes, this is expected. Most programming languages only consider 0 to be false.



        Elaborated



        This makes sense when you consider how -1 is represented in the processor. Typically you convert a positive number to it's negative counterpart by using 2's complement. 2's complent represents a negative binary number by inverting all the bits and adding 1. Say we have a 4-bit architecture, the decimal 1 is represented as 0001. To get its negative representation, invert all the bits and add 1, thus 0001 becomes 1110 + 1 = 1111.



        When we want to know if a value is false, we can or all its inputs, if the result is 0, it's false, otherwise it's true, and thus it makes perfect sense that -1 == true.



        The reson that we use 2's complement, as appose to 1's complement (where we just invert all bits without adding 1) is exacly because we do not want to be able to represent both 0 and -0.






        share|improve this answer













        TL;DR



        Yes, this is expected. Most programming languages only consider 0 to be false.



        Elaborated



        This makes sense when you consider how -1 is represented in the processor. Typically you convert a positive number to it's negative counterpart by using 2's complement. 2's complent represents a negative binary number by inverting all the bits and adding 1. Say we have a 4-bit architecture, the decimal 1 is represented as 0001. To get its negative representation, invert all the bits and add 1, thus 0001 becomes 1110 + 1 = 1111.



        When we want to know if a value is false, we can or all its inputs, if the result is 0, it's false, otherwise it's true, and thus it makes perfect sense that -1 == true.



        The reson that we use 2's complement, as appose to 1's complement (where we just invert all bits without adding 1) is exacly because we do not want to be able to represent both 0 and -0.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 8:34









        CortexCortex

        7010




        7010



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295735%2fpython-bool-1-is-returning-true-why-does-this-happen%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto