views:

83

answers:

6

Hey,

I'm writing a program that assigns prime numbers to each entry of a matrix and then I'll need to multiply some of them. The resulting number grows rapidly and I'm at a loss as to which type to use, as I'm getting "wrap-around" with long double :S

All the help is appreciated.

-Pickel

A: 

You may have to implement your own big integer type. Check out:

BigInt

dicroce
A: 

If it's just integers, there's long long int (at least in C/C++). If we're talking about doubles... Use a BigDecimal class.

I get the same problem with long long int. I'll check a BigDecimal one then.
Pickel
A: 

Assuming you are using "long double" as a type that it's a C or C++ assignment?

How big are your numbers?

http://stackoverflow.com/questions/1582529/maximum-value-vs-size-of-long-and-double-in-net

discusses some related stuff but may be beyond the scope of your assignment

Generally speaking you need an arbitrary precision library:

http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

But chances are that for purposes of an assignment you are not required to make things so complicated as to require the use of an APL

Elf King
it's a C assignment. For instance, for a 6x6 matrix I have to multiply 7*29*79*89*97*101*113*127*131*137*139*149*151.
Pickel
A: 

if it's an integer, use the BigInteger class (in Java or .NET)

if it's a floating point, use BigDecimal (only in java, .net still does not have a arbitrary precision floating number

if you're in C/C++, you have to create your own type

Louis Rhys
+3  A: 

Unless you're required to implement your own arbitrary precision type, use GMP. You'll want the mpz_t (integer) type. It's pretty well documented, and there are tutorials and StackOverflow questions you can look at.

Matthew Flaschen
thank you! that sounds complicated and I'm sort of a noob! I just need it for a tiny part of the project. would it be easier yo just create my own type?
Pickel
I mean *to just create my own type
Pickel
Probably not. I would only consider making your own for educational value, not to save time.
Matthew Flaschen
+1 for using an existing library. Writing your own would take a lot of time - it's not difficult, I'd just call it messy.
casablanca
I didn't mean make my own library. I just need a type and to be able to multiply and divide them
Pickel
A: 

If you refuse to use a library, and don't want to invent your own big types, why don't you keep track of extra factors of 2, or something like that.

while ( mybignum > BIGNUM_THRESH )
{
    twos++;
    mybignum /= 2; // use >>=1 if you use an integer type (you said you used double so therefore the /=)
}

then print out your answers as mybignum * 2**twos

Take a 64 bit int for twos and you're safe up to 2^2^64

mvds
I think I get what you mean. quick question though, if bignum is larger than the threshold it shows up negative so mybignum> BIGNUM_THRESH = 0, right?
Pickel
because of that "wrap-around" thing I mean
Pickel
no that's totally not the point! floating point types work like the scientific notation, with base 2: like Y*2^X, 0<Y<1. The bits available are shared between X and Y. The number of bits N for X determines the maximum value to 2^2^N. My suggestion was just a quick hack to emulate this method, but with a (much) bigger N.
mvds
I've come up with an alternative solution but for argument's sake, I need only prime numbers so if each number is an integer Y multiplied by a power of 2, they wouldn't be primes :(
Pickel