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.


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