- long exponent = expt;
- double intpart;
- int k = 0;
- while (mantissa != 0.0 && k++ < maxiter)
- {
- mantissa *= width;
- mantissa = modf(mantissa, &intpart);
- num_ <<= shift;
- num_ += (long)intpart;
- exponent -= shift;
- }
- if (exponent > 0)
- num_ <<= exponent;
- else if (exponent < 0)
- den_ <<= -exponent;
- } else {
- sign_ = 0;
+
+ const int FACT = 1 << 20;
+
+ /*
+ Thanks to Afie for this too simple idea.
+
+ do not blindly substitute by libg++ code, since that uses
+ arbitrary-size integers. The rationals would overflow too
+ easily.
+ */
+
+ num_ = (unsigned int) (mantissa * FACT);
+ den_ = (unsigned int) FACT;
+ normalise ();
+ if (expt < 0)
+ den_ <<= -expt;
+ else
+ num_ <<= expt;
+ normalise ();
+ }
+ else
+ {
+ num_ = 0;
+ den_ = 1;
+ sign_ =0;
+ normalise ();