Subject: Re: handling small numbers

From: "Richard A. O'Keefe" <[email-masked]>
Date: Fri, 5 May 2000 10:43:12 +1200 (NZST)

Tim Menzies <[email-masked]> wrote:
	got an error in my prolog code in the 20th decimal place and was wondering
	was i can do about it. ...
	my current approach is to code it all as fractions doing gcd and
	defer real number calcs till as late as possible,

This leaves you with a couple of problems.
First, exact rational calculations tend to give you large
numerators and denominators, and SWI Prolog arithmetic is,
um, a bit unpleasant there.
	?- X is 1000*1000*1000*1000.
	X = 1e+12 		% why isn't it 1000000000000?
	Yes			% maybe that's how bignums print??
	?- X is 1000*1000*1000*1000, integer(X).
	No			% nope, it realy did float it.
So if you calculate your numerators and denominators _directly_,
SWI Prolog is going to screw you with floating point arithmetic
anyway.

You might pick up the DEC-10 Prolog library, which contains a package
LONG.PL for doing rational arithmetic.  That may need a wee bit of
tuning; IIRC it assumes that 10**10 can be represented as an integer
(because on a DEC-10, it _could_), so you may have to change the
magic number 100000 to 10000 or even 1000 throughout.
But that still leaves you with the problem of converting an exact ratio to
a floating point number, and there you run into the problem that
exact integers can be too big to fit into a floating-point number.
That wasn't a problem in DEC-10 Prolog, because DEC-10 Prolog didn't
have floating point arithmetic.  I didn't worry about it at Quintus
simply because I never thought of it.

Someone posting from a *.edu site may possibly have access to a numerical
analyst.  There is a numerical analysis newsgroup; sci.num-anal if I
remember correctly.  Posting a more detailed description of the calculations
there may result in some useful advice.