[Issue 1976] New: Integral pow does not except negative powers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 6 23:40:29 PDT 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 at digitalmars.com
        ReportedBy: samukha at 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;
}
----


-- 



More information about the Digitalmars-d-bugs mailing list