C++ ugh...

C++ ugh...

Post by flava on Thu Jul 30, 2009 1:19 pm
([msg=27672]see C++ ugh...[/msg])

So C++... I'm rewriting a Python program of mine into C++ and I'm trying to use % on huge numbers. Python has no problem storing a 73 digit int, but if I try to store something with more than 9 digits, C++ starts freaking out. So I'm looking for a way to store as many digits as I want in an int with C++. I'm trying to avoid overloading the % operator to work with doubles because that seems like it would make some crazy overhead. Can someone help?
Viking at heart.
User avatar
flava
New User
New User
 
Posts: 31
Joined: Sun Jul 19, 2009 2:00 pm
Blog: View Blog (0)


Re: C++ ugh...

Post by mischief on Thu Jul 30, 2009 5:16 pm
([msg=27687]see Re: C++ ugh...[/msg])

C++ has no native way of handling 'big numbers'. you're going to need to either write a way to handle them yourself or use a library for it. a good candidate for that is GMP, the GNU Multiple Precision library. :)

http://gmplib.org/
http://en.wikipedia.org/wiki/GNU_Multi- ... on_Library
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: C++ ugh...

Post by flava on Fri Jul 31, 2009 3:25 pm
([msg=27721]see Re: C++ ugh...[/msg])

Thanks, GMP is awesome. Next question, I want to create classes using a loop

Code: Select all
char numberOfClasses = 'c';
for(int x = 'a';x <= numberofClasses; x++) {
    myClass x;
}

This obviously doesn't work because every iteration, it tries to redefine class x. Is there any way to do this?
Viking at heart.
User avatar
flava
New User
New User
 
Posts: 31
Joined: Sun Jul 19, 2009 2:00 pm
Blog: View Blog (0)


Re: C++ ugh...

Post by mischief on Fri Jul 31, 2009 3:51 pm
([msg=27723]see Re: C++ ugh...[/msg])

dynamic memory, look into the 'new' operator.

edit: you can also use a container to hold some objects, but that's dynamic in it's own way anyways.
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests