This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: numerical instability and estimate-probability


> 
> > 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]