• Advertisement
Sign in to follow this  

large numbers in c++

This topic is 3479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Of the libraries listed here, is there one that stands out from the rest for performance critical C++ code?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement