Thread: long long's what gives?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    13

    long long's what gives?

    Hi,

    I'm doing something that needs to use a 64 bit integer in c++. I assume this would be the long long. My question is when you use any of the standard operators, *,/,%,etc. you get zero if your integer is bigger than INT_MAX. How do I multiply and divide these things? Is this the 64 bit int I should be using? Any help is greatly appreciated.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I assume this would be the long long.

    Well don't. A long long is not a standard type, and even if it was there likely wouldn't be a guarantee on the size.

    >> My question is when you use any of the standard operators, *,/,%,etc. you get zero if your integer is bigger than INT_MAX.

    Post a compilable example of the problem.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Are you sure?
    The reason I ask is that until very recently, printf() in Windows used a non-standard operator of %I64d, instead of %lld. (I believe that %lld was introduced with Vista, but on this point I could be wrong. It was definitely absent in 98.) If you're printing out the number using the wrong modifiers, you'll certainly get the wrong answer, and possibly corrupt your stack.

    That said, something like:
    Code:
    long long n = 5000000000LL;
    n /= 2;
    should do the right thing. Post a minimal problematic example if you have troubles.
    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)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    long long int a = 12345LL;
    Add the correct suffix code to your constants to make them the right type to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    13
    Quote Originally Posted by Salem View Post
    Code:
    long long int a = 12345LL;
    Add the correct suffix code to your constants to make them the right type to begin with.
    I'm not using constants. I have something like:

    Code:
    long long totalSize = (long long)(pow(2,inputParameterPassedFromConsole));
    avgSize = totalSize/numProcessors;
    numOverloadedCPUs = totalSize%numProcessors;
    I am also going to be doing this which will GENERATE integers bigger than INT_MAX through multiplication and division (and other stuff) of input variables.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm not using constants. I have something like:

    Well is pow giving you the correct result?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I am also going to be doing this which will GENERATE integers bigger than INT_MAX through multiplication and division (and other stuff) of input variables.
    That's fine. INT_MAX has little to do with long longs. You will be fine as long as you don't overflow a long long.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > long long totalSize = (long long)(pow(2,inputParameterPassedFromConsole));
    Using pow() to calculate powers of 2 just SUCKS.

    1. It's horribly inefficient.
    2. doubles only have about 50 or so bits of precision, so for very large numbers you just get inaccurate results.

    The quick way?
    Code:
    long long totalSize = 2LL << inputParameterPassedFromConsole;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> The quick way?

    Excellent idea! But the calculation is slightly off - should be:

    1LL << inputParameterPassedFromConsole
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by maverick_starst View Post
    long long's what gives?
    please note that the apostrophe is NEVER used in the english language to modify a noun to plural form. it seems like english is your first language, otherwise I would say nothing about it. the apostrophe is used to join words into contractions and to modify a noun to possessive form. I see misuse of the apostrophe far too often, and it irritates me to see people do it when I know for a fact they were taught the right way in school. when I was in school, we learned in 3rd grade how to use the apostrophe, and how to construct a plural noun. since most people frequenting this board are at least chronologically older than that, I would expect that most should know this.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Elkvis View Post
    please note that the apostrophe is NEVER used in the english language to modify a noun to plural form. it seems like english is your first language, otherwise I would say nothing about it. the apostrophe is used to join words into contractions and to modify a noun to possessive form. I see misuse of the apostrophe far too often, and it irritates me to see people do it when I know for a fact they were taught the right way in school. when I was in school, we learned in 3rd grade how to use the apostrophe, and how to construct a plural noun. since most people frequenting this board are at least chronologically older than that, I would expect that most should know this.
    Also, note that both proper nouns (eg: English) and the first word of a sentence should be capitalized.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    please note that the apostrophe is NEVER used in the english language to modify a noun to plural form. it seems like english is your first language, otherwise I would say nothing about it. the apostrophe is used to join words into contractions and to modify a noun to possessive form. I see misuse of the apostrophe far too often, and it irritates me to see people do it when I know for a fact they were taught the right way in school. when I was in school, we learned in 3rd grade how to use the apostrophe, and how to construct a plural noun. since most people frequenting this board are at least chronologically older than that, I would expect that most should know this.
    Apostrophes ARE used to make plural forms.
    Apostrophe - The OWL at Purdue
    Apostrophes are used to form plurals of letters that appear in lowercase; here the rule appears to be more typographical than grammatical, e.g. "three ps" versus "three p's." To form the plural of a lowercase letter, place 's after the letter.
    There is no need for apostrophes indicating a plural on capitalized letters, numbers, and symbols (though keep in mind that some editors, teachers, and professors still prefer them).
    Yes, many people "mis-use" it for other things, but I think it's more of a historical and conventional thing, and they know perfectly well that apostrophes are usually not used to pluralize.

    When I see a 's for pluralization, I take it as a clarification that it is for pluralization. It may not be so clear otherwise with abbreviations for example. If you have MP3s, and you didn't previously know what MP3 means, you could be misled to think "s" is part of the abbreviation.

    I'm not sure if my interpretation is the common interpretation, since English is not my native language.
    Last edited by cyberfish; 07-28-2009 at 07:17 PM.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    13
    Quote Originally Posted by Elkvis View Post
    please note that the apostrophe is NEVER used in the english language to modify a noun to plural form. it seems like english is your first language, otherwise I would say nothing about it. the apostrophe is used to join words into contractions and to modify a noun to possessive form. I see misuse of the apostrophe far too often, and it irritates me to see people do it when I know for a fact they were taught the right way in school. when I was in school, we learned in 3rd grade how to use the apostrophe, and how to construct a plural noun. since most people frequenting this board are at least chronologically older than that, I would expect that most should know this.
    I also wrote the title in like half a second and didn't bother reviewing. However, I do find grammar nazi's (mwahahaha) to be incredibly mis-guided individuals. Feynman has some really amusing anecdotes about how silly such people are. Also, no, grammar isn't explicitly taught in schools any more; (ooh a semi-colon) that time is now used for actually reading books. Take it up with the school board but, to me, lamenting the "death of the english language" is as silly as lamenting the "death of old norse". It never ceases to amaze me how full of themselves grammar nazis get. Especially in a bloody internet forum. You really think you're getting a fix on peoples education? I don't know about you but I frequently write homonyms of what I meant to type as well as make punctuation and spelling errors when typing, and it's really not worth the time to fix them. I bet you find words like "dunno" or the addition of "ness", "y" or "like" to the end of words where it is not normally added to be a corruption of the english language. Anywho, (mwahaha) if finding grammar mistake gets you hot I guess a c++ forum is a good place to troll.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    13
    Anyways, thanks for the help everybody. I seem to have it working now but I'm not entirely sure where the problem was. I just went through it all and made sure everything was explicitely cast (like (long long)(x)=(long long)(y)*int(z) and it seems to give the correct result now.

  15. #15
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    is that for real!!
    i googled it and there are more like this (as far as i could remember)
    long long int

    and other four ones :S !!

    do they have useful uses ? what are they ?
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM