Thread: c++ help

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Question c++ help

    hey, i have about 3 years experience in php so im no stranger to programming and the c++ syntax is not new to me at all. I have been dabbling in c++ here and there. anyway, i am making a program to calculate the amount of clicks needed to zero the scope on my airsoft gun (not a real gun). i just need to know if there is a function that could turn a negative number into a positive? also, there is a php manual that is basically an index of all php functions (http://www.php.net/manual/en/) i was wondering if there was a c++ equivalent or anything similar?

    thanks for your time, evan

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Quote Originally Posted by evanr
    i just need to know if there is a function that could turn a negative number into a positive? also, there is a php manual that is basically an index of all php functions (http://www.php.net/manual/en/) i was wondering if there was a c++ equivalent or anything similar?
    You probably want to do this...:

    15 => -15

    In PHP you can do like this..:

    PHP Code:
     <?php 
    $i 
    15;
    $neg_i = -$i;
    echo 
    $neg_i;
    ?>
    In C++ there isn't much difference...

    PHP Code:
     #include <iostream> 
    using namespace std;
     
    int main()
    {
    int i 15;
    int neg_i = -i;
    count << i;
    return 
    0;

    About functions list....

    C++ is combined with libraries, so there is impossible for functions list like PHP does to exists...

    Altough at this site (cprogramming.com) exists small, but often useful list: http://www.cprogramming.com/function.html
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, there is std::abs(int) in the <cmath> header. cppreference.com is a decent reference. And, SGI has a good STL reference.
    Last edited by Zach L.; 08-23-2005 at 08:59 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    If you want a book with tangable pages, try the O'reilly "in a nutshell" series. I've done well with them for a few other languages. but not sure what the c++ version is like.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I'd definitely recommend dinkumware's C++ library reference and full index.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    wow, thanks for the helpful info, my prog is working fine now...

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Ah, I didnt know that worked publikum, thanks.

    I've just been using: neg_i = i - i - i; (seems to work in all cases).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Quote Originally Posted by Dae
    I've just been using: neg_i = i - i - i; (seems to work in all cases).
    Even for this you could use better way...

    neg_i = i - 2*i
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by publikum
    Even for this you could use better way...

    neg_i = i - 2*i
    Thats not a better way, its a different way. :P Two operations are caried out either way, and the same amount of characters typed too.

    However if the compiler is optimized and uses a bitshift for the multiplication part it might be a microsecond faster. But neg_i = -i; is definetly faster since ints just use the last bit to determine if its negative or not, so only that bit would have to be changed to turn it negative.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Actually, Dae, (perhaps I'm misunderstanding what you were saying, but) the two operations are not equivalent. If i is already negative, saying -i will make it positive. I'd say that your method for determining absolute negative value works well. Of course, the obvious solution is always available: #define ABS_NEG(x) ((x) < 0 ? (x) : -(x))
    Last edited by LuckY; 08-23-2005 at 01:16 PM.

  11. #11
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If you didn't know that -i worked, well, 0 - i would be better than i - i - i.

    -i doesn't change just one bit, because while the last bit determines the sign, flipping it does not flip the sign. Usually, to flip the sign, you need to bitwise flip the integer and then increment. At least that's one way it can be done. Of course a processor might have an instruction that does this faster than bitwise flipping the integer and then incrementing, but I don't know anything about what instructions processors have.

    Flipping the sign bit just adds 2^7 for chars, 2^15 for shorts, or 2^31 for longs to the number (or subtracts that amount if positive). Or in general, 2^(bitlength - 1)

    This way of representing signed integers is interesting because it means that addition, subtraction, and multiplication can be done with the same algorithm for signed and unsigned values.
    Last edited by Rashakil Fol; 08-23-2005 at 01:30 PM.

  12. #12
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by LuckY
    Actually, Dae, (perhaps I'm misunderstanding what you were saying, but) the two operations are not equivalent. If i is already negative, saying -i will make it positive. I'd say that your method for determining absolute negative value works well. Of course, the obvious solution is always available: #define ABS_NEG(x) ((x) < 0 ? (x) : -(x))
    Yeah I dont think I meant that. By two operations I meant i - i - i vs i - 2 * i, in the first its two negative (-) operations on i, on the second its a negative (-) and a multiplication (*) operation. The operations may not be equivalent but they produce the same outcome. -i produces the same outcome too, but its definetly not going through the same procedure as the other ways to do it.

    If you didn't know that -i worked, well, 0 - i would be better than i - i - i.
    I forgot about that way, I've seen it and I believe used it once though. Thats probably what the compiler does for -i eh? seems like the best way to do it.

    -i doesn't change just one bit, because while the last bit determines the sign, flipping it does not flip the sign. Usually, to flip the sign, you need to bitwise flip the integer and then increment. At least that's one way it can be done. Of course a processor might have an instruction that does this faster than bitwise flipping the integer and then incrementing, but I don't know anything about what instructions processors have.
    Oh I didnt know that. I just assumed thats how it was done since.. well.. that would make sense, but theres probably some incapability (or less efficiency) for doing it that way (in the details).

    I didnt start this topic, but thanks for the info guys.. learning as I go along.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Some signed integer representations, and the popular two's complement representation.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed