[Issue 2636] std.math.pow should be a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 30 08:56:19 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2636





------- Comment #1 from andrei at metalanguage.com  2009-01-30 10:56 -------
(In reply to comment #0)
> Currently, pow is an overloaded function, which always takes real as the first
> argument.
> 
> However, real isn't a common variable type, so passing something that can be
> implicitly casted to real results in a compiler overload resolution error.
> 
> This problem could be solved by implementing pow as a wrapper to the original
> function (named e.g. _pow):
> 
> real pow(T, U)(T t, U u)
> {
>    return _pow(cast(real)t, u);
> }
> 
> This should take care of cases where you try to pass a double, int, or even
> possibly a custom struct that supports an opCast to real.
> 
> I'm not sure if casts are needed for the second argument, if they are, special
> care should be taken to not cast integral types to real, as the integer
> versions are optimized for that.
> 

I had the following in my tree for a while, just committed it now so you can
look at it:

F pow(F)(F x, uint n) if (isFloatingPoint!(F))
F pow(F)(F x, int n) if (isFloatingPoint!(F))
F pow(F)(F x, F y) if (isFloatingPoint!(F))

Would this be enough?


-- 



More information about the Digitalmars-d-bugs mailing list