large numbers in c++

Started by
7 comments, last by theacclaimed 15 years, 9 months ago
Recently I started working on the problems at project euler, and being a beginner it has really motivated me and given me things to figure out on my own. I'm not sure if I'm putting this in the best section, but my problem is with using large numbers in C++. One problem I'm trying to do is to add 50 digit numbers. I don't need all significant figures, just some. But when I put a 50 digit number in C++ I get the error: "constant too big". This is when using the double data type, and I've tried other things which produced the same error. Another problem is to add up the digits of 2 to the 1000th power. As a double, I can get about 15 significant figures of the value before they become all zeroes. With this problem specifically, I would like to know how to get those significant digits if it is possible, but this also confuses me about the first problem. I get a number here about 300 digits long, but I can't use 50 digit numbers in the same data type? Any help would be appreciated, and I am new to the forums (although I have read them a bit) so sorry if this thread is in the wrong spot.
Advertisement
Link
As you have found out C++ has no support for arbitrarily large numbers unlike some languages like Python!
You'll either need to write your own like Moonshoe suggested or use a 3rd party library as mentioned in my link:

On a computer, arbitrary-precision arithmetic, also called bignum arithmetic, is a technique whereby computer programs perform calculations on integers or rational numbers (including floating-point numbers) with an arbitrary number of digits of precision, typically limited only by the available memory of the host system. Using many digits of precision, as opposed to the approximately 6–16 decimal digits available in most hardware arithmetic, is important for a number of applications as described below; the most widespread usage is probably for cryptography used, e.g., in every modern web browser.

[Edited by - daviangel on July 10, 2008 9:33:32 PM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
I'm trying to use GMP and with this code I get errors like:

unary minus operator applied to unsigned type, result still unsigned
unresolved external symbol ___gmpz_add referenced in function _main

2 more unresolved external symbols.

I've read the documentation, and I'm doing what it says to.

#include <iostream>#include <cmath>#include <iomanip>#include <gmp.h>using namespace std;int main(){		mpz_t sum;	mpz_t num1, num2;	mpz_init_set_str (num1, "71329612474782464538636993009049310363619763878039", 10); 	mpz_init_set_str (num2, "10848802521674670883215120185883543223812876952786", 10);	mpz_add(sum, num1, num2);	cout << sum << endl;	system("pause");	return 0;}
Quote:Original post by theacclaimed
Another problem is to add up the digits of 2 to the 1000th power. As a double, I can get about 15 significant figures of the value before they become all zeroes. With this problem specifically, I would like to know how to get those significant digits if it is possible, but this also confuses me about the first problem. I get a number here about 300 digits long, but I can't use 50 digit numbers in the same data type?


Because 50 digit numbers have 50 significant figures. The double type is an approximation of a numeric value, which allows it to represent very large numbers (as well as numbers very close to zero, and decimals in general) without using correspondingly more space in memory. (Well, actually a double typically uses 8 bytes instead of 4, but that is still a lot less than would be needed to represent 2^1000 exactly. See if you can reason out for yourself how much memory that would take. Hint: how would you write this number in base 256?)

When you write the statement that initializes your double variable, you're still trying to initialize it with an integral constant. The compiler complains because you have told it to create the concept of that large number, convert it to a double (i.e. special, approximate representation of the number as a potentially-real one instead of a necessarily-integer one), and store that. And it can't fathom that 50-digit-integer concept in the first place.

You can't get more than 15 significant figures out of the double representing 2^1000, for the simple reason that that is all the precision that is available.
Quote:Original post by theacclaimed
I'm trying to use GMP and with this code I get errors like:

unary minus operator applied to unsigned type, result still unsigned
unresolved external symbol ___gmpz_add referenced in function _main

2 more unresolved external symbols.

I've read the documentation, and I'm doing what it says to.

*** Source Snippet Removed ***


How are you compiling? You need to link in the gmp library, e.g.

g++ -o program program.cpp -lgmp
I don't know. I got the files from here http://cs.nyu.edu/exact/core/gmp/ because I'm using windows and the gmp site only has files for linux. I downloaded 2 library files and a header file and I just put them in the appropriate places. I'm not really sure what else to do.
Of the libraries listed here, is there one that stands out from the rest for performance critical C++ code?
All I had to do was put .0 at the end of the numbers, thanks for the great advice.

This topic is closed to new replies.

Advertisement