Bignums Fast and Dirty

I had a need the other day for dealing with some really big numbers. Unfortunately, the machine I was using did not have mpexpr installed. Woe is me. Until I came up with the following fast and very dirty workaround. Works on Unix systems and Windows systems with Cygwin:

proc mpexpr { args } {
    return [ exec echo $args | bc -l ]
}

% mpexpr 12342342354564636*94852304570297207525
1170699616126044020647089347918085900

Is it a cheat? Well, yeah, it's a cheat. But it was darn handy!

CMcC: I've written bignum, a quick critcl wrapper for libgmp, giving arbitrary precision ints and floats.

SS: I've written tclsbignum, Tcl bindings for sbignum (bignum C implementation). Note that sbignum's C implementation itself is mine, and I'll be happy to release it under the same license as Tcl if it can be useful to put bignums in the core. The lib is not as complete or fast as GPM, but is mostly API compatible and much much more simple, and performances are acceptable. It contains all the useful primitives needed for a programming language (including sqrt, powers, powers mod N, GCD, and so on).

You can find the library at http://www.hping.org/tclsbignum .

IDG: It builds ok on Suse 8.1 (gentables needs -lm ) but when I try to load the .so, I get undefined symbol __fixunsdfi. Any ideas?

SRIV 2004-06-14 I added libgcc to the makefile on line 40

$(LD) -G -z text -o tclsbignum.so tclsbignum.o sbignum.o sbignum-tables.o -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/ -lgcc -ldl -lc -lm 

Make adjustments for your systems location of libgcc.


Sarnold: See tcllib for math::bignum (integers) and math::bigfloat (for floating-point numbers).


DKF: Time to learn a bit more exec magic:

proc mpexpr args {
    exec bc -l <<[join $args]
}

No discussion of fast and dirty cheats for big numbers would be complete without Big Floating-Point Numbers.