Long or double in c++

Posted on 2011-10-26
Last Modified: 2012-05-12
I have a calculation which gives me a result of:4391552000.

I have an int trying to store this and when I output it to the debugger I get:25866.

The result is this in the output debug window:
25866 = 1000 * (548944 * 8) / 3734

Open in new window

I have read tha the max number an int can hold is: 4294967 296.

Question 1.
What exactly is happening to turn my number from 4391552000 to 25866.

Question 2.
What should I use, a long or a double??
Question by:Wanting2LearnMan
    LVL 33

    Accepted Solution

    Question 2:
    You should use a Double. It can store numbers up to 15 digits. Int only gives the maximum number of 2147483647.
    LVL 84

    Assisted Solution

    96584704/3734  = 5866

    Do longs in your c++ implementation hold at least 33 bits?

    LVL 53

    Assisted Solution

    Assuming a 32bit unsigned int, (1000 * (548944 * 8)) overflows to 96584704, which becomes 25866 when divided by 3734.

    It's probably best to look at the hexadecimal representations of the values :

            decimal                                                             hexadecimal

            548944                                                            0x00086050
            548944 * 8 = 4391552                                    0x00430280
            548944 * 8 * 1000 = 4391552000                  0x105C1C400

    That last value is too big for a 32 bit integer, so it's truncated to 0x05C1C400 :

            truncated -> 96584704                                    0x05C1C400
            96584704 / 3734 = 25866                              0x0000650A

    >> What should I use, a long or a double??

    That depends entirely on your purpose.

    If you want an integer value, check if your platform supports a 64bit unsigned integer type (uint64_t eg.).
    If you want a floating point value, then double should do fine.
    LVL 13

    Assisted Solution

    by:Hugh McCurdy
    I like Infinity08's answer but would like to add my own questions.

    Could you share your purpose?  I'll tell you to use 64 bit integers if you are writing guidance software for something like a Patriot Missile.  You'll get more questions if you say this is for a class.

    Please do not give me any points for this post.
    LVL 32

    Assisted Solution

    the microsoft compiler has a MulDiv function for this common problem. it takes three int arguments number, numerator and denominator and returns an int.  number and numerator were multiplied using a 64-bit integer temporary and the result of the multiplication was divided by the denominator.

    if you want to keep the code portable, you easily can make such a helper yourself.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now