Like Tree6Likes

Bool returning function gives me a non binary value

This is a discussion on Bool returning function gives me a non binary value within the C++ Programming forums, part of the General Programming Boards category; I just started testing my program and I expect -of course- that there are some logical errors I have to ...

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Bool returning function gives me a non binary value

    I just started testing my program and I expect -of course- that there are some logical errors I have to truck down.

    So, pointInLeaf returns a bool, which is then returned by pointInside and finally returned by another function (which is the named pointInside, but overloaded with one argument and public scope, while the others are of private scope. All functions belong to same class).

    Code:
    bool Quadtree::pointInside(const Point3d& p)
    {
        bool bla = pointInside(root, p);
        std::cout<< "here1 |" << bla <<"|" << std::endl;
        return bla;
    }
    
    bool Quadtree::pointInside(node*& node, const Point3d& p)
    {
        if(!node->child[0]) // Is node a leaf?
        {
            bool bla = pointInLeaf(node, p);
            std::cout<< "here2 |" << bla <<"|" << std::endl;
            return bla;
        }
        ...
    }
    and look at my output
    Code:
    here2 |1|
    here1 |208|
    The value 208 varies. Before it was 76.

    There is probably something wrong in the algorithm(just started to test the program), but this does not explain this.

    EDIT:
    Check my 2nd post, for two lines of more information.
    Last edited by std10093; 08-20-2013 at 10:19 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Code:
    ...
    What happens when this section executes ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The function is sure to go into the body of this loop, for the tests I am performing now.

    I should however mention that, when the program performs tests that I already know that they work, it goes as it should be, giving back a binary value.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Even if pointInLeaf returns an int converted to bool and is inlined, the result should still be a bool and printed as such. I'm not sure if a bool must be printed as 0 or 1 though, and unfortunately that is a part of the standard that I find a bit more challenging to interpret, heh.

    Therefore, my best guess is that you have undefined behaviour in your program that lies in code that you did not show.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Does this give any unexpected results?

    Code:
    #include <iostream>
    
    bool foo();
    bool bar();
    
    bool foo()
    {
        bool poop = bar();
        std::cout << "foo() " << poop << std::endl;
        std::cout << "foo() direct" << bar() << std::endl;
        return poop;
    }
    
    bool bar()
    {
        bool poop;// = 208;
        std::cout << "bar() "<< poop << std::endl;
        return poop;
    }
    
    int main(void)
    {
        foo();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,897
    I've always thought a bool is one of two values zero or not zero.
    From the C++11 working draft

    Document Date: 2012-02-28 Number: N3376
    Section 4.12
    A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
    prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
    any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of
    type bool; the resulting value is false.
    It looks like you should be testing/printing true/false not the integral value. When printing you should probably set the boolalpha manipulator to print true or false instead of the integral value.

    Jim

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by jimblumberg
    I've always thought a bool is one of two values zero or not zero.
    No, what you quoted are the rules for conversion to bool, not the rules as to the valid values of bool, or how a bool value should be printed. Refer to clause 4.7 instead for how a bool would be converted to an integer type... but that still does not cover precisely how a bool would be printed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    I'm having trouble locating the relevant section in the standard, but I suspect that the values for bool, beyond what the standard says in 4.12, are implementation defined.
    Code:
    namespace life
    {
        const bool change = true;
    }

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elkvis View Post
    I'm having trouble locating the relevant section in the standard, but I suspect that the values for bool, beyond what the standard says in 4.12, are implementation defined.
    2.14.6 Boolean literals
    3.9.1 Fundamental types p. 6
    4.5 Integral promotions p. 6

    All suggest bool must be either true (1) or false (0)


    But
    48) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an
    uninitialized automatic object, might cause it to behave as if it is neither true nor false.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,897
    Quote Originally Posted by laserlight View Post
    No, what you quoted are the rules for conversion to bool, not the rules as to the valid values of bool, or how a bool value should be printed. Refer to clause 4.7 instead for how a bool would be converted to an integer type... but that still does not cover precisely how a bool would be printed.
    True but Section 4.7 doesn't really tell you much IMO.
    Section 4.7 Integral conversions.
    If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source
    integer (modulo 2 where n is the number of bits used to represent the unsigned type). [ Note: In a two’s
    complement representation, this conversion is conceptual and there is no change in the bit pattern (if there
    is no truncation). — end note ]

    If the destination type is signed, the value is unchanged if it can be represented in the destination type (and
    bit-field width); otherwise, the value is implementation-defined.

    If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and
    the value true is converted to one.
    The conversions allowed as integral promotions are excluded from the set of integral conversions.
    So it seems important to know if bool is signed or unsigned, and how many bits the bool can contain. Depending on how bool on the system in question is defined the conversion from an int to a bool could lead to an implementation defined value.

    Jim
    Last edited by jimblumberg; 08-21-2013 at 09:57 AM.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    Quote Originally Posted by SirPrattlepod View Post
    2.14.6 Boolean literals
    3.9.1 Fundamental types p. 6
    4.5 Integral promotions p. 6

    All suggest bool must be either true (1) or false (0)
    thanks for the pointers. I only have the 2011 draft standard, not the final standard, but in 4.5 p6, it says "A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true
    becoming one." so it appears that the values of true and false are very clearly and well-defined. however it also appears the value of an uninitialized bool object still remains undefined. the size of storage assigned to an object of type bool appears to be implementation-defined. on some compilers, it may be 1 byte, while on others, it may be equal to the size of an int, or any other variation that allows a set of two values.
    Code:
    namespace life
    {
        const bool change = true;
    }

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Elkvis View Post
    thanks for the pointers. I only have the 2011 draft standard, not the final standard, but in 4.5 p6, it says "A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true
    becoming one." so it appears that the values of true and false are very clearly and well-defined. however it also appears the value of an uninitialized bool object still remains undefined. the size of storage assigned to an object of type bool appears to be implementation-defined. on some compilers, it may be 1 byte, while on others, it may be equal to the size of an int, or any other variation that allows a set of two values.
    I agree.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Sirplattelbot, that's not the case. These are uninitialized variables!

    Here is the function's missing part, which, eventually is executed once.
    Code:
    bool Quadtree::pointInside(node*& node, const Point3d& p)
    {
        if(!node->child[0]) // Is node a leaf?
        {
            bool bla = pointInLeaf(node, p);
            std::cout<< "here2 |" << bla <<"|" << std::endl;
            return bla;
        }
        std::cout<< "entered " << std::endl;
        if(!node->child[7]) std::cout << "NULL" << std::cout; // This is NOT null
        pointInside(node->child[0 + (p.x() >= node->split.x())        // The index results to 7
            + 2 * (p.y() >= node->split.y())
            + 4 * (p.z() >= node->split.z())], p);
    }
    output
    Code:
    entered
    here2 |1|
    here1 |24|
    24            // that's the output from main function
    So, our result from this, is that the when printed, the bool value is not guaranteed to be binary?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    is it possible that pointInLeaf is returning an uninitialized bool?
    Code:
    namespace life
    {
        const bool change = true;
    }

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thought I should mention this earlier. Sorry.
    No, pointInLeaf, returns true or false only. You can see that the cout in line 6, gives as a value equal to one.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 03:59 PM
  2. bool typedef and binary compatibility
    By BattlePanic in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 09:04 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 06:38 PM
  4. bool function
    By MB1 in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2005, 06:31 PM
  5. Object returning a bool
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 11-27-2002, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21