How I can calculate numbers in C++

  • Context: C/C++ 
  • Thread starter Thread starter Jameson
  • Start date Start date
  • Tags Tags
    C++ Numbers
Click For Summary

Discussion Overview

The discussion revolves around how to perform calculations with numbers that exceed the precision limits of standard libraries in C++. Participants explore various methods for handling multi-precision arithmetic, including custom implementations and existing libraries.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Jameson inquires about calculating numbers with more than 19 digits, expressing a need for greater precision.
  • One participant suggests using multi-precision arithmetic, recommending the storage of digits in arrays or classes and performing arithmetic on these separate digits.
  • Another participant proposes using string inputs to handle calculations, detailing a method for adding numbers by aligning their lengths and processing each digit individually.
  • A participant mentions declaring a 64-bit variable using __int64, noting uncertainty about its digit capacity.
  • Discussion includes the maximum values for signed and unsigned 64-bit integers, with specific numerical examples provided.
  • Clarifications are made regarding the use of __int64 in Microsoft compilers versus alternatives like int64_t or long long in other environments.
  • A participant expresses frustration with the complexity of the topic, indicating a sense of overwhelm.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with no consensus on a single solution or method. The discussion remains unresolved regarding the best way to handle calculations beyond standard library limits.

Contextual Notes

Some methods discussed depend on specific compiler environments, and there are unresolved considerations regarding the implementation of multi-precision arithmetic.

Jameson
Insights Author
Gold Member
MHB
Messages
4,533
Reaction score
13
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.

Jameson
 
Technology news on Phys.org
Jameson said:
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.

Jameson

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.
 
Hi,
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,
 
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.
 
The largest unsigned 64bit integer is 2^64-1 = 18446744073709551615

The largest signed 64bit integer is 2^63-1 = 9223372036854775807
 
Last edited:
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:
Hurkyl, your right. The signed figure is still correct.
 
Headache. . . headache. . .
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 14 ·
Replies
14
Views
8K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 27 ·
Replies
27
Views
17K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 29 ·
Replies
29
Views
9K
Replies
81
Views
8K