# [ADOL-C] Bug in extreme cases of pow()

Kshitij Kulshreshtha kshitij at math.upb.de
Thu Jul 31 12:12:59 EDT 2014

Hello,

you are assuming that the implementation of std::pow() gives the
mathematically correct result for the case x=0,e=0. It does not,  0^0 is
mathematically not defined. Check Wolframalpha if you don't believe me.
std::pow() returns 1 if e=0 for all x without checking x, so even
std::pow(NAN,0) returns 1. You don't suppose we should provide a
derivative for that :-) std::pow(0,e) for any nonpositive e returns an
Inf. The Implemetation checks for this rule later. But both those rules
together imply that pow(0,0) is itself a NaN.

Therefore dy/dx for x=0,y=0 is mathematically a NaN.

For case 3: x=0, e=1 in order to find dy/dx do not use e (adouble) but
ev (double) in pow.

y = pow(x,ev)

Then y.getADValue(0) will return 1. Also y = 0^e is not differentiable
at e=0 due to 0^0 being undefined, so dy/de is a NaN.

As on 2014-07-31 17:33h, Samuel Leweke did write:
> Hi Kshitij,
>
> Thanks for your fast response! From the results I got from ADOL-C I
> already guessed the inner workings you've pointed out.
> However, as I've explained in my first mail the results are
> mathematically wrong (or am I missing something?).
>
> Is there a workaround I can use to obtain the correct / desired results
> (maybe by directly manipulating the internal values via setADValue() )?
>
> Best,
> Sam
> Am 31.07.2014 11:15, schrieb Kshitij Kulshreshtha:
>> Hello,
>>
>> for y = pow(x,e) we apply the rule for forward differentiation in your
>> given direction (\dot{x},\dot{e}). You get:
>>
>> \dot{y} = e*pow(x,e-1)*\dot{x} + log(x)*pow(x,e)*\dot{e}
>>
>> Now if you put x = 0, e = 0, you get a NaN from the first, and a -Inf
>> from the second term. Result is NaN.
>>
>> If you put x = 0, e = 1, you get NaN from the second term. Total result
>> is NaN.
>>
>> Cheers.
>> Kshitij
>>
>> As on 2014-07-31 10:43h, Samuel Leweke did write:
>>> Hi,
>>>
>>> I've noticed a bug in some special cases of the pow() function (see the
>>> code attached).
>>> The troubling statement is y = pow(x, e). The output for several values
>>> of x and e (admittedly extreme cases) is given below:
>>>
>>> x = 0, e = 0
>>> y = 1
>>> dy/dx = nan
>>> dy/de = nan
>>> ======================
>>> x = 1, e = 0
>>> y = 1
>>> dy/dx = 0
>>> dy/de = 0
>>> ======================
>>> x = 0, e = 1
>>> y = 0
>>> dy/dx = nan
>>> dy/de = nan
>>> ======================
>>> x = 1, e = 1
>>> y = 1
>>> dy/dx = 1
>>> dy/de = 0
>>>
>>> Let's check the values produced in each test.
>>> Test 1: Since 0^0 = 1 and x^0 = 1 for all x != 0, dy / dx should be 0
>>> here. The value of dy / de is correct because the derivative does not
>>> exist (0^e is discontinuous at e = 0).
>>> Test 2: This is correct.
>>> Test 3: For fixed e, the function x^e is differentiable and its
>>> derivative dy / dx is 1 for x = 0 and e = 1. For fixed x = 0, we have
>>> 0^e = 0 for all e > 0. Hence, we should get dy / de = 0 in this case.
>>> Test 4: This is correct.
>>>
>>> Am I doing something seriously wrong or is this really a bug? Is there a
>>> work around for this bug to get the correct behavior?
>>>
>>> Best regards,
>>> Samuel Leweke
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------------------------
>>>
>>>
>>> ------------------------------------------------------------------------------------------------
>>>
>>>
>>> Forschungszentrum Juelich GmbH
>>> 52425 Juelich
>>> Sitz der Gesellschaft: Juelich
>>> Eingetragen im Handelsregister des Amtsgerichts Dueren Nr. HR B 3498
>>> Vorsitzender des Aufsichtsrats: MinDir Dr. Karl Eugen Huthmacher
>>> Geschaeftsfuehrung: Prof. Dr.-Ing. Wolfgang Marquardt (Vorsitzender),
>>> Karsten Beneke (stellv. Vorsitzender), Prof. Dr.-Ing. Harald Bolt,
>>> Prof. Dr. Sebastian M. Schmidt
>>> ------------------------------------------------------------------------------------------------
>>>
>>>
>>> ------------------------------------------------------------------------------------------------
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>>
>
> _______________________________________________

--
Dr. Kshitij Kulshreshtha

Institut für Mathematik,