Refactor the number tower
liancheng opened this issue · comments
Current situation
Current number tower implementation is too lame and cumbersome, which makes arithmetic procedures actually a mess.
Currently, there are only three kinds of numbers:
- Small integer: 30-bit (or 62-bit on x64) immediate signed integer value.
- Fixnum: boxed exact complex number, represented by two
mpq_t
. Fixnum is used to represent all exact numbers that cannot fit in a small integer, i.e., exact big integers, exact rationals, and exact complexes. - Flonum: boxed inexact complex number, represented by two
double
. Flonum is used to represent all inexact numbers. Yes, there's no immediate version of inexact number.
The plan
The plan is to add two new number types:
- Fixreal: boxed exact rational number, represented by a
mpq_t
, used to represent exact big integers and exact rationals. - Floreal: boxed inexact rational number, represented by a
double
, used to represent inexact rationals.
Correspondingly, Fixnum and Flonum will be merged into 1 Complex type, which is represented by two rational numbers real
and imag
under the coat of rsexp
.
Invariables
- The real part and the imaginary part of a Complex number must be both exact numbers or inexact numbers. For exact Complex,
real
andimag
must be small integer or Fixreal; for inexact Complex,real
andimag
must be Floreal. r_exact_p
should returnTRUE
for small integers, Fixreals and exact Complexr_inexact_p
should returnTRUE
for Floreal and inexact Complex
Normalization
When creating Fixreals, input will be checked to see whether it can fit into a small integer, if yes, a small integer will be returned. This process is called Fixnum normalization.
On the other hand, Flonums won't be normalized automatically. But normalization do happens when r_inexact_to_exact
is called with a Flonum. For example, when evaluating (inexact->exact 1.0)
, resulted exact integer 1
is represented by a small integer, rather than a Fixreal.
Added a new branch finer-number-tower
to fix this issue.
The number tower has been fixed commit 5ccd831. Arithmetic procedures hasn't been updated yet.
I think this issue can be closed now. The number tower is fully refactored, and all the previously implemented arithmetic procedures have been re-implemented with the new number tower (see 2180d9b).