www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1976] New: Integral pow does not except negative powers

reply d-bugmail puremagic.com writes:
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;
}
----


-- 
Apr 06 2008
next sibling parent d-bugmail puremagic.com writes:
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


-- 
Apr 06 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1976





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


-- 
Apr 06 2008
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
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?


-- 
Apr 07 2008
parent Max Samukha <spambox foo.com> writes:
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.
Apr 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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.


-- 
Apr 07 2008
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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?
Apr 08 2008