Schema

Bill Gribble grib@gnumatic.com
Wed, 13 Dec 2000 10:15:06 -0600


On Tue, Dec 12, 2000 at 06:04:08PM -0500, David Merrill wrote:
> Would you please give me an example that uses both numerator and
> denominator in each field? Or a set of example, if that is easier?
> I'm having trouble seeing how this works.

I'm not sure what you mean by "uses".  The C data type of the damount
and of the value is 'gnc_numeric', which is an opaque type.  You can
do things like add, subtract, multiply, divide values of type
gnc_numeric.  As an implementation detail (partially exposed in the
API), every gnc_numeric has an integer numerator and an integer
denominator.

The reason for this is mainly to guarantee exact representations of
fractional values; even double-precision floats accumulate error
pretty quickly since they can't exactly represent most fractional
numbers.  For example, if you accumulate double-precision 1.01 one
million times (representing the sale of a million widgets at $1.01,
for example), you will get pretty close to a penny error (this is a
result of the base-10 to base-2 conversion; the fractional value .01
is exact in base-10 but is a continued fraction in base-2, which the
floating point number is).

If you represent "1.01" as "101 / 100" instead, you will never have
this problem, until you run out of room in the integer numerator,
which is not likely to happen soon :)  

You can get around the problem using tricks with doubles (rounding and
truncating after every operation) too, but the API complexity is just
as high (and error prone), there are real range limitations with
double's ability to count exactly, and we felt more comfortable using
a guaranteed-exact integer representation.

Bill Gribble