Bug 2973 – std.math.pow(int, int), etc.

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-05-13T09:52:00Z
Last change time
2015-06-09T01:27:57Z
Assigned to
andrei
Creator
dsimcha

Comments

Comment #0 by dsimcha — 2009-05-13T09:52:51Z
std.math should have a function for efficiently and accurately computing int^int, int ^ uint, etc. Casting to floating point is not a good solution b/c you lose performance and, in some cases, precision. Furthermore, this would be dead simple to implement.
Comment #1 by rinick — 2009-07-21T01:02:35Z
(In reply to comment #0) > std.math should have a function for efficiently and accurately computing > int^int, int ^ uint, etc. Casting to floating point is not a good solution b/c > you lose performance and, in some cases, precision. Furthermore, this would be > dead simple to implement. agree int^uint and long^uint are useful. but int^int is not necessary, because: int a = pow(2,-1); this doesn't make any sense.
Comment #2 by andrei — 2009-07-21T19:19:38Z
We currently have: pure nothrow F pow(F)(F x, uint n) if (isFloatingPoint!(F)); pure nothrow F pow(F)(F x, int n) if (isFloatingPoint!(F)); F pow(F)(F x, F y) if (isFloatingPoint!(F)); I think we're in good shape. We could add overloads for raising integrals to unsigned powers but I fear confusion due to wraparound. The magnitudes of exponents for which efficiency could become an issue are also those that would wraparound the result. If no compelling arguments come up, I'll close this soon.
Comment #3 by dsimcha — 2009-07-21T19:55:36Z
Couldn't you just stick an assert in there to make sure it doesn't wrap around? I haven't tested, but I would think that even in debug mode, this would be faster than using the float version.
Comment #4 by andrei — 2009-07-21T20:15:20Z
(In reply to comment #3) > Couldn't you just stick an assert in there to make sure it doesn't wrap around? > I haven't tested, but I would think that even in debug mode, this would be > faster than using the float version. 1. Please test. What powers are you looking at? For most bases and most exponents the result will go off range. 2. I prefer returning a floating point number instead of attempting to return an integer and failing dynamically, all for a doubtful speed benefit.
Comment #5 by rinick — 2009-07-21T23:22:10Z
(In reply to comment #4) > What powers are you looking at? For most bases and most > exponents the result will go off range. long a = foo(); long b = a * a * a; I don't want to call foo() again. So I have to define one more variable just because pow doesn't accept integer. (In fact, I have a longPow function and I use it from time to time)
Comment #6 by clugdbug — 2009-08-11T04:58:06Z
> Casting to floating point is not a good solution b/c > you lose performance and, in some cases, precision. Are you sure? Performance: Floating point multiplication is typically the same speed or faster than integer multiplication on most CPUs from the past 15 years. On most Intel CPUs, integer multiplication is actually done in the floating point unit. Precision: You'll only lose precision if the base is greater than the 1/real.epsilon. For an 80-bit real, this means precision is lost only when the result is exactly equal to ulong.max. And the only time that can happen is with pow(ulong.max, 1). (because ulong.max is divisible by 5, but not by 25).
Comment #7 by dsimcha — 2009-10-18T07:51:24Z
Since I filed this one and I'm thoroughly convinced now that it's not necessary and noone else seems to want it, I'm resolving it as WONTFIX.
Comment #8 by andrei — 2009-10-18T13:06:15Z
(In reply to comment #7) > Since I filed this one and I'm thoroughly convinced now that it's not necessary > and noone else seems to want it, I'm resolving it as WONTFIX. Thanks for following through.
Comment #9 by rinick — 2009-12-04T22:17:46Z
Now we have power operator. It will be very confusing that 3.0^^3 works but 3^^3 doesn't. Most people would think ^^ in D is just like ** in python.
Comment #10 by clugdbug — 2010-02-12T00:16:38Z
FIXED: 3^^3 works in DMD 2.040.