This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: numerical instability and estimate-probability
- From: Jan Hubicka <jh at suse dot cz>
- To: moshier at moshier dot ne dot mediaone dot net
- Cc: guerby at acm dot org, gcc at gcc dot gnu dot org, jh at suse dot cz
- Date: Fri, 16 Nov 2001 17:31:48 +0100
- Subject: Re: numerical instability and estimate-probability
- References: <Pine.LNX.4.20.0111161103160.9996-100000@moshier.ne.mediaone.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.
Actualy in this case we are speaking about frequencies (for probabilities
it is just OK to use 0 ... 10000 integers).
The frequencies can get very huge, so only sane workaround wihtout FP
is IMO only inventing the mantisa/exponent at my own :(
Honza