Thread: -3 to 3 need code..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    cppreference.com has a bare bones listing of many parts of the C++ standard library, and its C legacy. SGI's Standard Template Library Programmer's Guide has a more comprehensive listing of the STL portion of the C++ standard library. MSDN is yet another online resource.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by indigo0086
    nm < 0 ? cout << -nm : cout << nm
    cout << (nm < 0 ? -nm : nm);

    Why does yours work? Don't << and >> have higher precedence than the ternary thing? (: and ?) So shouldn't it evaluate both cout << 's before even caring about the ternary, causing both -3 and 3 to output? (It doesn't, though...)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #18
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    Yes, the Ternary Operator works nicely, but this interesting thread wouldn't have developed if abs() hadn't been brought up!

  4. #19
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Edit: After looking online I found this

    Precedence Problems

    The iostream library is designed to allow a minimum of parentheses:

    cout << "a+b = " << a+b << endl;

    The + operator has higher precedence than the left shift operator, so we get the desired parse; a+b is calculated first, then the result is sent to cout.

    cout << a ? f() : g();

    Here, the use of C++'s only ternary operator gets us into trouble, but it's not that ?: is ternary; it's that it has lower precedence than <<. Therefore, we're asking the compiler to generate code to shift a to cout, then use the result of that expression as the condition in the ?:. The tragic aspect of this situation is that the code is perfectly legal! (An output stream object like cout has a member operator void * that can be used implicitly to convert it to a void * value, which in turn can be converted to false or true, depending on whether the pointer value is null or not.) Here's a case where we reach for our parentheses:

    cout << (a ? f() : g());

    If you want to be considered completely normal, you can take things a step further:

    if( a )
    cout << f();
    else
    cout << g();
    so a safer code would be...

    Code:
    cout <<  (nm < 0 ? -nm :  nm);
    it just seems to be one of those things that just works.
    Last edited by indigo0086; 06-23-2006 at 02:20 PM.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Why does yours work? Don't << and >> have higher precedence than the ternary thing? (: and ?) So shouldn't it evaluate both cout << 's before even caring about the ternary, causing both -3 and 3 to output? (It doesn't, though...)
    The understanding I have comes from the syntactic form of the conditional operator.

    cond ? expression_1 : expression_2;

    expression_1 and expression_2 are primary-expressions on their own right.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Quote Originally Posted by Mario F.
    abs() is declared in the cstdlib header. Not the math header

    It could probably (it should) have been declared in the math header. For historical reasons it was decided against it. There's only a handful more functions that have this little quirk. However, if you take a look at the long integer and double versions of this function (labs() and fabs(), respectively) you will find that they are declared in the math header.

    So take again a good look again at the link provided by Indigo. You will see abs() requesting the cstdlib.
    Maybe my stdlib is old. I have both abs() and labs() in there.

    Can anyone tell me why this works(or if it works on their compiler)? :\

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
        int x = 500;
        x = labs(x);
        if(isdigit(x))
    
        cout << "Hello World";
        cin.get();
        return 0;
    
    }

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The standard library and STL headers are not "closed" entities. The objects and methods implemented by the iostream demand including other header files from the standard library.

    From that simple example you provide, you can already see that cstdlib and cmath are two of those included headers. A few others of the top of my head are locale, stdexcept, exception, string...

    EDIT: As for labs, it is in fact a cstdlib function. My bad. fabs is the one defined in cmath.
    Last edited by Mario F.; 06-24-2006 at 04:07 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM