-3 to 3 need code..

This is a discussion on -3 to 3 need code.. within the C++ Programming forums, part of the General Programming Boards category; cppreference.com has a bare bones listing of many parts of the C++ standard library, and its C legacy. SGI's Standard ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    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.
    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

  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 03:20 PM.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,581
    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 05:07 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

Page 2 of 2 FirstFirst 12
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, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08: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, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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