www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.math.sqrt(real x) && std.math.sqrt(float x) overloads

reply "Tobias Pankrath" <tobias pankrath.net> writes:
Shouldn't this just work?

--


struct Point
{
     int x, y;
     Point opBinary(string op)(Point rhs)
     {
         return mixin("Point(x " ~ op ~ " rhs.x, y " ~ op ~ " 
rhs.y)");
     }
}

double dist(Point a, Point b)
{
     return sqrt(abs(a.x - b.x) ^^ 2 + abs(a.y - b.y) ^^ 2);
}

Instead it's
Error: function std.math.sqrt called with argument types:
         ((int))
matches both:
         std.math.sqrt(float x)
and:
         std.math.sqrt(real x)
May 09 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Tobias Pankrath:

 Shouldn't this just work?
 ...
 Instead it's
 Error: function std.math.sqrt called with argument types:
         ((int))
 matches both:
         std.math.sqrt(float x)
 and:
         std.math.sqrt(real x)

In theory yes, in practice probably it's better for it to not compile, given the current situation of the D type system. Bye, bearophile (this is probably one of the less informative answers I've given recently)
May 09 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, May 09, 2012 21:39:38 Tobias Pankrath wrote:
 Shouldn't this just work?
 
 --
 
 
 struct Point
 {
 int x, y;
 Point opBinary(string op)(Point rhs)
 {
 return mixin("Point(x " ~ op ~ " rhs.x, y " ~ op ~ "
 rhs.y)");
 }
 }
 
 double dist(Point a, Point b)
 {
 return sqrt(abs(a.x - b.x) ^^ 2 + abs(a.y - b.y) ^^ 2);
 }
 
 Instead it's
 Error: function std.math.sqrt called with argument types:
 ((int))
 matches both:
 std.math.sqrt(float x)
 and:
 std.math.sqrt(real x)

It's amibigous, so no it doesn't just work. There was a discussion in what to do about it a few months back. I don't remember all of the details, but you can read it here: http://forum.dlang.org/thread/j7mt9q$1tsa$1 digitalmars.com - Jonathan M Davis
May 09 2012