Re: Invalid results from exp2 function (C math library)



On Jun 6, 11:30 pm, pcauchon <pcauc...@xxxxxxxxx> wrote:
On Jun 7, 3:19 pm, user923005 <dcor...@xxxxxxxxx> wrote:





On Jun 6, 11:09 pm, pcauchon <pcauc...@xxxxxxxxx> wrote:

On Jun 7, 2:52 pm, "bigcaterpil...@xxxxxxxxx"

<bigcaterpil...@xxxxxxxxx> wrote:
The value x implies exp2(x) has overflow. You can use exp(x) to try.

When I try with the standard exp function I do get normal results:
exp(-x) is always equal to 0 when x is larger than about 750.

I get the issue I described earlier only with the exp2 function.

GIGO.
Has it occurred to you that exp(-x) is not equal to zero when x is
larger than 750?
It seems strange that it should make you happy.
You are asking your floating point library to do something it isn't
able to do.

#include <math.h>
#include <stdio.h>
int             main(void)
{
    double          dexp = exp(-750);
    double          dback = log(dexp);
    printf("%.20g should be 750.0 but is it?\n", dback);
    return 0;

}

I agree with you it's not symetric. Anyway very few floating point
operation have exact symetric inverse and that's a well-known
limitation.

In my specific case I only take exponent of negative values. I would
expect the result of exp(x) or exp2(x) for x <= 0 to be between 0 and
1. I do not really care if I have an absolute error less than say
10**-10, so computing 0 instead of the exact 10**-483 or whatever the
value is doesn't bother me.

I have problems when I get a negative result from exp2 or when the
result magnitude is larger than 1. From what I see I could get both
even if it clearly doesn't make any sense.

I worry that you are inserting values larger than -2000 into exp2() in
the first place. That is not too different from inserting -1 into
sqrt() or tan(pi/2) or any other value that is not going to be
representible because the domain does not make sense for the given
hardware. If you have values that extreme, why are you taking
negative exponentials of them?

Once intermediate calculations underflow, you should not expect
subsequent calculation to make sense.
Consider my example of exp(log(x)). Mathematically, the answer is x,
but if I insert a huge or tiny x, then the answer will no longer have
any meaning. I expect that some intermediate calculation hurled
chunks when you told it to take the exponential of something smaller
than is possible for the hardware.

Any clue on how to detect when a floating point underflow occurs?

It depends on your compiler. On some compilers you can set floating
point exceptions to have implementation defined behavior.
.



Relevant Pages

  • Re: c vs java floating point errors under windows XP
    ... > of a floating point calculation. ... The precision of the numbers must be ... > algorithm in java and in C to accomplish this calculation. ... MinGW is the wrong compiler to use, ...
    (comp.programming)
  • Re: demonic problem descriptions
    ... It's not even USUALLY a disaster in common practice (which is to say, ... The most insidious thing about floating ... you have a better understanding of the representation than the ... calculation you make whose error estimate (which you won't compute, ...
    (comp.lang.lisp)
  • Re: Testable Predictions by HdB
    ... > Jesse F. Hughes wrote: ... > floating point numbers often do not. ... This makes the Testable Predictions ... there are situations in which the calculation will give wrong ...
    (sci.math)
  • Re: fixed-point
    ... First ask yourself "what is the purpose of a floating point number?" ... Adjust the numbers so that both numbers have the same exponent ... The 16 in the macro refers to the multiplication factor. ... function call in so far as the paramter conversion is concerned. ...
    (comp.lang.c)
  • Re: Floating point arithmetic question
    ... If I do this calculation in floating point arithmetic (of some specific ... The precison of the 'side with respect to a given plane' test result ... precision calculation will give you wrong answer. ...
    (comp.graphics.algorithms)

Loading