Combining integer and float types problem.

David Gillies daggillies at gmail.com
Wed Sep 29 09:26:11 CEST 2010


On Wed, Sep 29, 2010 at 12:33 AM, soggyoats at gmail.com
<soggyoats at gmail.com> wrote:
[...]
> Anyway, I have a program where at one give point a variable may have to be used to calculate 10000!, and at another point it may be used to calculate 57/292. What I have found is that using float types give a bunch of problems (I think the reason why is because float types don't expand? I may be wrong). For example, with a float type initialized to the default precision, 10000! ends up being something like 25939292 followed by and ungodly amount of zeros. It's not even close to the correct amount. So, I was wondering, is there some way to make a universal data type? One that it is possible to do any operation to, without having to consider precision and the like? I guess a good comparison would be the python bignum library, which does pretty much exactly what I'm looking for, but just isn't GMP (which is altogether, in my opinion, much better).
[...]


I suppose you could use GMP as a universal arithmetic type. The amount
of glue you'd have to place round each operation would probably make
it unfeasibly slow (although I infer that your very asking this
question demonstrates a certain amount of naivete as to the legitimate
scope of multi-precision arithmetic, and thus you may be well-served
by a tinker-toy wrapper around GMP.) The C++ classes work rather well
for turning algorithms in papers into code you can show your head of
department as proof of concept, but subtleties have a habit of
becoming less subtle as argument sizes get bigger. MPFR is great for
hiding a lot of the complexity of floating point stuff (and especially
for doing the right thing with rounding, which in most cases trumps
pure precision.)

I suppose the bottom line is, if it were easy to create a transparent,
efficient, portable representation of reals without regard to
precision or accuracy someone would have done it. Like most things,
you might get two out of three. But GMP is not Mathematica (yeah, I
know, some internals, etc...) If you don't care about precision your
problem is almost certainly ill-formulated.

By the way, what do you mean '10000! is followed by an ungodly amount
of zeros'? All factorials greater than 4! end in zero. 10000! ends in
2499 zeros, as should be blindingly obvious from a couple of minutes
with pencil and paper.

-- 
David Gillies
San Jose
Costa Rica


More information about the gmp-discuss mailing list