# Calculation results with very long numbers

• 09-07-2010
Calculation results with very long numbers
Hi. I am new to C++.
Learning from tutorials.
One of tutorials shows how to write a simple program that accepts a number and then calculates it to power of another user specified number. Simple numbers work fine (2^5=25, etc) But I want to try very long numbers (1234567^34854322).
The program either quits after accepting the two numbers, or shows something like 1.#INF.
I realize the result may take a hundred lines to display, that is fine.
Question. Is this a failure to calculate or failure to display a result that has been calculated?
How do I format the result so it can be seen?
What are the limitations, if any?
Here is the program:

Code:

```// Compute power of a number #include <iostream> #include <cmath> #include <iomanip> int main() {     using namespace std;     double power1;     double num1;     cout << "Enter number" << endl;     cin >> num1;     cout << "Enter power" << endl;     cin >> power1;     num1 = pow(power1,num1);     cout << setprecision(130)  << num1 << endl;         system("pause");         return 0; }```
• 09-07-2010
Syscal
I am certain that 1234567^34854322" is way too big for any standard data type. You could write one, storing digits in a vector. Then compute the result based on that old way you learned to count. Some digit * the base ^ postion
• 09-07-2010
What is the best source to look up these custom datatypes?
Syscal, thank you for the reply.
What is the best source to look up these custom vector data types? Is this the right term, first of all?
Thanks again.
• 09-07-2010
msh
It's a failure to store. double datatype just doesn't have enough bits to represent such a huge number.

See here for how to determine various such limits on your system.
• 09-08-2010
BMJ
This might be enlightening: Arbitrary-precision arithmetic - Wikipedia, the free encyclopedia

I must be going crazy because I can remember there being a Boost bigint library... but it doesn't exist.
• 09-08-2010
iMalc
Hmm, as fate would have it, I've been working on my simple and easy to understand bigint and megafloat classes over the last few days.
Probably best to pick one of those existing ones if you're more interested in results, than how it works though.
• 09-08-2010
BMJ (thank you BTW) pointed to a wikipedia article which has this link to an online calculator: ttmath.org / Big online calculator . So, it can compute but what code do I write to generate these large numbers? examples will be appreciated!
• 09-11-2010
How to connect these two codes?
I found an example of how to use vectors, but this example below uses a constraint. How do I connect the two so it stores long numbers as shown above:

Code:

```#include <cstring> // memcpy #include <vector> #include <cstdio> // printf int main() {   using namespace std;   const char arr[] = "12345678902333333333323323333333345345345345345345435";   // construct a vector with 11 zero-initialized chars.   vector<char> vec(sizeof arr);    // copy 11 chars from arr into the vector   memcpy(&vec[0], arr, sizeof arr);   // prints "1234567890"   printf("%s", &vec[0]);       system("pause");     return 0; }```
• 09-12-2010
chameleons
Quote:

I found an example of how to use vectors, but this example below uses a constraint. How do I connect the two so it stores long numbers as shown above:

what do you mean by constraint?
https://mattmccutchen.net/bigint/
• 09-12-2010
C_ntua
Your problem here is pow(). You need to re-implement the function.

Basically, you want
Code:

`vector<char>* long_pow(double num, double pow);`
Now how to implement that. I don't think it is that easy. Your alternative would be to find a ready library.

You could of course go with
Code:

`vector<char>* long_pow(vector<char>& num, vector<char>& pow);`
and basically do it as you would do it with a pen and paper speed not being good but still it would work