This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: numerical instability and estimate-probability
- From: Stephen L Moshier <moshier at mediaone dot net>
- To: guerby at acm dot org
- Cc: gcc at gcc dot gnu dot org, jh at suse dot cz
- Date: Fri, 16 Nov 2001 11:12:35 -0500 (EST)
- Subject: Re: numerical instability and estimate-probability
- Reply-to: moshier at moshier dot ne dot mediaone dot net
> If the issue is just counting and multiplying, a simple struct with an
> int for mantissa and another for exponent (x = int1 * 2^int2) should
> do the job as a short term fix IMHO.
Here is a specific suggestion, assuming you have numerator and
denominator expressed as integers and you want to calculate
(numerator * 10000) / denominator
without overflow.
Say there is a maximum representable numerator, 4294967295, so if numerator
is greater than 4294967295 / 10000 = 429496 then numerator * 10000 would
overflow. In that case divide both numerator and denominator by
a quantity such as (numerator / 429496) + 1 to avoid overflow.
In other words,
if (numerator > 429496)
{
c = (numerator / 429496) + 1;
numerator /= c;
denominator /= c;
}
answer = (numerator * 10000) / denominator;
Assuming this was a probability, denominator >= numerator and the
roundoff error committed in dividing through by c is small.