Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ help

  1. Mar 4, 2005 #1
    Who can tell me how I can calculate numbers to as many digits as I see fit? The normal libraries can only hold 19 or so, which is waaayyy less than I want. Any help is appreciated.

  2. jcsd
  3. Mar 4, 2005 #2


    User Avatar
    Science Advisor
    Homework Helper

    You need to use multi-precision arithmetic. You can write your own or can get the code elsewhere. Essentially, the digits of a number (real or integer) are stored separately in arrays, classes whatever. And you do the arithmetic on the separate digits. There are elegant algorithms for doing subtraction, multiplication, division, and addition in multi-precision. Should be able to google for the algorithms.
  4. Mar 5, 2005 #3


    User Avatar
    Homework Helper

    I think what you need is inputs of string. Then you can analyze the string and calculate them. For example, you want calculate 35 + 244
    You will ask the user give you 2 strings "35", and "244", and the operator as well, i.e. :"+".
    And here is what I did:
    + Make the 2 numbers have the same length "035", and "244"
    + Use the loop to convert to number and calculate each degit backwards, i.e. : 5 + 4, 3 + 4, and 0 + 2.
    + Print out the result.
    It's a little bit more tricky if you work with rational numbers like "3.5", "2.44".
    I finished the addition, subtraction, as well as multiplication, but not in C++, I programmed in DB. If you want, I can show you my work.
    Viet Dao,
  5. Mar 5, 2005 #4
    You can declare a 64-bit variable using

    __int64 variable;

    I'm not sure how many digits that will allow, but it's pretty big.
  6. Mar 5, 2005 #5


    User Avatar
    Staff Emeritus

    The largest unsigned 64bit integer is 2^64-1 = 18446744073709551615

    The largest signed 64bit integer is 2^63-1 = 9223372036854775807
    Last edited: Mar 5, 2005
  7. Mar 5, 2005 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Of course dduardo means 2^64 - 1 = 18446744073709551615 :smile:

    __int64 will only work in microsoft compilers. (As will _int64 and int64)
    In *nix systems, you'll want int64_t, or uint64_t. (they're in <inttypes.h>, I think)
    For many compilers, long long will do it.

    But as mentioned, you'll want to find a big number package.
    Last edited: Mar 5, 2005
  8. Mar 5, 2005 #7


    User Avatar
    Staff Emeritus

    Hurkyl, your right. The signed figure is still correct.
  9. Mar 9, 2005 #8
    Headache. . . headache. . .
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C++ help
  1. Some C++ help please (Replies: 6)

  2. C-Programming Help (Replies: 6)

  3. C++ help. (Replies: 2)

  4. C++ for loop help (Replies: 6)

  5. C++ program help (Replies: 2)