c++ numbers out of range

In this forum members can discuss topics about specific programming languages.
Post Reply
latinpower
Posts: 2
Joined: Mon Nov 15, 2010 1:00 am

c++ numbers out of range

Post by latinpower »

i'm new here :D

i was wondering if you could tell me how can i solve this problem.

for some excercises, i need to work with big numbers, but sometimes the console gives me numers like 1.150-e1651 etc.

so i can't work with big numbers,

i only know int, float, double but none of this worked for me,

maybe i'm doing something wrong. :)
harryh
Posts: 2091
Joined: Tue Aug 22, 2006 9:33 pm
Location: Thessaloniki, Greece

Re: c++ numbers out of range

Post by harryh »

Welcome to PE latinpower!

C++ also has 64-bit integers ("long long" or "__int64" depending on which implementation you use).
As a principle, you don't need any bigInt or bigNum libraries to solve the PE problems (but you do need to know the limitations imposed by each type and take those limitations into consideration when you formulate your solutions).
vindvaki
Posts: 1
Joined: Fri Dec 10, 2010 9:18 pm

Re: c++ numbers out of range

Post by vindvaki »

For integers: On Linux/Unix you can use GMP and on Windows you can use MPIR.
browni3141
Posts: 18
Joined: Thu Jan 20, 2011 2:06 am

Re: c++ numbers out of range

Post by browni3141 »

harryh wrote:Welcome to PE latinpower!

C++ also has 64-bit integers ("long long" or "__int64" depending on which implementation you use).
As a principle, you don't need any bigInt or bigNum libraries to solve the PE problems (but you do need to know the limitations imposed by each type and take those limitations into consideration when you formulate your solutions).
How do you suggest solving problems like 16 and 97 without bignums?
Image
TripleM
Posts: 382
Joined: Fri Sep 12, 2008 3:31 am

Re: c++ numbers out of range

Post by TripleM »

97 doesn't need big integers whatsoever.

I guess you could say 16 does; but to be honest if you didn't have a big integer library, writing some code to calculate what is required is less code than you'll need for most other problems. It's just another algorithm.
User avatar
euler
Administrator
Posts: 4138
Joined: Sun Mar 05, 2006 4:49 pm
Location: Cheshire, England
Contact:

Re: c++ numbers out of range

Post by euler »

browni3141 wrote:How do you suggest solving problems like 16 and 97 without bignums?
Way back when PE started many programming languages did not have native big number capabilities - nor were such libraries readily available - so the purpose of producing some of those earlier problems was to get people to think about how these fundamental arithmetic operations (including modulo arithmetic) is tackled. Problem 16 should be a fun example which allows you to create your own multiplying algorithm.

To explicitly state it... the difference, which Harry mentions, is that generally for those earlier problems the actual problem itself is the "big number" calcuation; this should be obvious from the problem. Whereas for the more recent problems the challenge itself is something quite different. So during problem development the size of integers required (or degrees of precision) are limited to common limitations of most programming languages. However, there may be rare occasions when this general principle needs to be overridden, but that would be stated in the problem.
Image
impudens simia et macrologus profundus fabulae
browni3141
Posts: 18
Joined: Thu Jan 20, 2011 2:06 am

Re: c++ numbers out of range

Post by browni3141 »

Thank you for your replies. I did enjoy coming up with an algorithm for those problems. I realize it would be ridiculous to try 97 with bignums.
Image
mdean
Posts: 185
Joined: Tue Aug 02, 2011 2:05 am

Re: c++ numbers out of range

Post by mdean »

Well, since I see there's already a topic on it (plus do we really have to abide by that one thread per programming language thing?), I was thinking I see at least a couple more problems where storing large numbers is going to be a must. I was wondering if instead of having to reinvent the wheel each time if it would be smarter to write up a class for this sort of thing and somehow include it in future programs that might need it. Which means I'll likely need to relearn how to do classes and figure out where my compiler looks for user-made include files...

As I said, I've spotted at least 3 where something like this might be handy. Anyone more familiar with the problems think something like this might be warranted? Without giving anything away, any suggestions on what kind of functionality I might want to include for such a class or should I just try to add to it as needed?
Image
suitti
Posts: 12
Joined: Fri Jul 08, 2011 8:13 pm

Re: c++ numbers out of range

Post by suitti »

I don't always use the same language to work out problems. Some of the languages, like Unix's bc, and scheme have big number support. Perl has a module you can load. There are C & C++ bignum libraries. So, while i worked out how to do problem 97 in C without bignums, I kicked it off in bc. I mean, i couldn't quite copy and paste the problem into bc - i had to type it in bc's syntax. But it could not have taken me more than a few seconds. Linux is really good at doing more than one thing at a time, so i started coding the other solution in another window. But bc beat me by quite a bit, generating over 2 million digits of the full answer, dumping the answer to a file.
brainiac1530
Posts: 16
Joined: Sun Mar 02, 2014 2:32 pm

Re: c++ numbers out of range

Post by brainiac1530 »

You can and should do 97 without big integers (a 64-bit type will do.) It should take almost zero processing time -- there is a log-time algorithm that is ideal for it, and easily written.
Post Reply