Thread overview
[Issue 1976] New: Integral pow does not except negative powers
Apr 07, 2008
d-bugmail
Apr 07, 2008
d-bugmail
[Issue 1976] Integral pow does not accept negative powers
Apr 07, 2008
d-bugmail
Apr 07, 2008
d-bugmail
Apr 08, 2008
Janice Caron
Apr 08, 2008
Max Samukha
Apr 07, 2008
d-bugmail
April 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1976

           Summary: Integral pow does not except negative powers
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: samukha@voliacable.com


Here is a modified version of pow that accepts both positive and negative powers:

----
real pow(real x, int n)
{
    real p = 1.0, v;

    if (n < 0)
    {
        switch (n)
        {
        case -1:
            return 1 / x;
        case -2:
            return 1 / (x * x);
        default:
        }

        n = -n;
        v = p / x;
    }
    else
    {
        switch (n)
        {
        case 0:
            return 1.0;
        case 1:
            return x;
        case 2:
            return x * x;
        default:
        }

        v = x;
    }

    while (1)
    {
        if (n & 1)
            p *= v;
        n >>= 1;
        if (!n)
            break;
        v *= v;
    }
    return p;
}
----


-- 

April 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1976





------- Comment #1 from samukha@voliacable.com  2008-04-07 01:42 -------
Created an attachment (id=242)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=242&action=view)
A pow patch for phobos


-- 

April 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1976





------- Comment #2 from samukha@voliacable.com  2008-04-07 01:49 -------
Corrected the summary


-- 

April 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1976





------- Comment #3 from andrei@metalanguage.com  2008-04-07 14:14 -------
How about pow called with an unsigned exponent larger than int.max?


-- 

April 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1976


samukha@voliacable.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #4 from samukha@voliacable.com  2008-04-07 15:40 -------
Right. My bad.


-- 

April 08, 2008
On 07/04/2008, d-bugmail@puremagic.com <d-bugmail@puremagic.com> wrote:
>  How about pow called with an unsigned exponent larger than int.max?

Overload it for both int and uint, and have the int version accept negative values?
April 08, 2008
Janice Caron Wrote:

> On 07/04/2008, d-bugmail@puremagic.com <d-bugmail@puremagic.com> wrote:
> >  How about pow called with an unsigned exponent larger than int.max?
> 
> Overload it for both int and uint, and have the int version accept negative values?

Yes, there is pow(real, int) in phobos that calls row(real, real) if the exponent is negative. Somehow the overload resolution error I got when calling pow with (double, int) made me think there is no such an overload. Anyway, though the proposed implementation may be faster for negative exponents, it doesn't work for int.min and so needs more thought.