Subject: Re: handling small numbers

From: Jan Wielemaker <[email-masked]>
Date: Fri, 5 May 2000 13:15:14 +0200

> 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.

SWI-Prolog its are 32-bits, so X is 1000*1000*1000*1000 has a problem.
There are two solutions: raise an exception or promote to a float (=
C double).  I've choosen for the latter for `convience'.  In many cases
you get more or less the proper result.  Only if you want to be precise
you'll need some extra checks and handle numbers with care.

This however works nice:

	X is (1000*1000*1000*1000)/200000, integer(X).

	X = 5000000

Please note that some of this behaviour depends on the `iso' prolog
flag, (actually I think it should raise an exception in ISO mode).

	Regards --- Jan