Re: Floating point performance issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[snip stuff I don't agree with, or rather, we probably agree but
have a different idea of what "in general" means; it all borders
on philosophy, and that is very off-topic here, so let's not :-) ]

For the OP's example, rounding towards zero does not give less
precise results.

I don't think this is true (or can you explain why?).

First off, only a very small range of inputs does not completely
explode to 0 or Inf, of course.  Or the smallest denormal ;-)

Let me give you a testcase:

--- 8< ---
#include <stdio.h>
#include <math.h>
#include <fenv.h>

#define S 20000000

static double calc(double f)
{
        int j;
        double a = 0;
        double b = 1;

        for(j = 0; j < S; j++) {
                a = b * f;
                b = a * f;
        }

        return a;
}

int main(void)
{
        double x = 1 - 1./(2*S);

        double c1 = calc(x);
        fesetround(FE_TOWARDZERO);
        double c2 = calc(x);

        printf("nearest = %.20g\n", c1);
        printf("down    = %.20g\n", c2);

        return 0;
}
--- 8< ---

It outputs:

nearest = 0.36787944637187158792
down    = 0.36787944526181193261

while we have

correct = 0.36787943657294925905

so both rounding modes lose about half of the available precision.
Round to zero actually did slightly better.

Now you can say that this is because x already is rounded from the
true value, this however is not the case: taking S=2**25, we get

nearest = 0.36787944391225085860
down    = 0.36787944204965455918

with

correct = 0.36787943843052687818


For 2**N multiplies, you lose about N bits of precision with
either rounding mode.

To get accurate results requires (much) more work.

If one wants accurate results to around 1 ulp, yes. However, without
this work, rounding to nearest is a bit better than directed rounding.

I actually thought in terms of single precision numbers, which would
give totally bogus results; but 64-bit loses more than half of the
bits already.  Surprisingly round to zero did a bit better, I didn't
expect that either (I thought it would be a bit or maybe two worse).
Rats, now I have to look it up, there must be literature on this :-)

Trapping the underflow exception may be another solution, e.g. to
branch to specific code when the first underflow occurs, but this
requires some work.

And in a real calculation, very much more work!


Segher



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux