How I can calculate numbers in C++

  • C/C++
  • Thread starter Jameson
  • Start date
  • Tags
    C++ Numbers
In summary, you need to use multi-precision arithmetic to calculate numbers to as many digits as you see fit.
  • #1
Jameson
Gold Member
MHB
4,541
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
  • #2
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.
 
  • #3
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,
 
  • #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.
 
  • #5
The largest unsigned 64bit integer is 2^64-1 = 18446744073709551615

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

1. How do I declare variables in C++ for calculations?

To declare a variable in C++, you must specify the data type and a name for the variable. For example, to declare an integer variable called "num", you would use the syntax: int num;

2. How do I perform basic arithmetic operations in C++?

C++ has built-in operators for basic arithmetic operations such as addition (+), subtraction (-), multiplication (*), and division (/). These operators can be used on variables or literal values to perform calculations.

3. What is the order of operations in C++?

The order of operations in C++ follows the same rules as in algebra. Expressions within parentheses are evaluated first, followed by multiplication and division from left to right, and finally addition and subtraction from left to right.

4. How can I round numbers in C++?

The C++ round() function can be used to round a number to the nearest integer. To use this function, you must include the cmath library in your code.

5. Are there any common errors to watch out for when calculating numbers in C++?

One common error is using the wrong data type for a variable, which can cause unexpected results or errors. It is important to make sure that all variables are properly declared and that the correct data type is used for each variable in a calculation.

Similar threads

  • Programming and Computer Science
Replies
22
Views
728
  • Programming and Computer Science
Replies
1
Views
344
  • Programming and Computer Science
Replies
1
Views
626
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
8
Views
866
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top