digitalmars.D.learn - stuck on opDiv / opBinary
- Spacen Jasset (24/24) Aug 30 2015 I have just added an opDiv to this class, but it doesn't seem to
- John Colvin (4/28) Aug 30 2015 Don't use opMul or opDiv etc, use opBinary
- BBasile (11/35) Aug 30 2015 try
- Spacen Jasset (4/16) Aug 30 2015 Ah yes, it's didnt' complain. But anyway that was the wrong
- Steven Schveighoffer (8/26) Aug 31 2015 Actually, that sounds like a bug to me. Trying to compare a string
- Timon Gehr (21/27) Aug 30 2015 import std.math: sqrt;
- Spacen Jasset (4/26) Aug 30 2015 Thanks, that is interesting. I am curently porting, rather than
I have just added an opDiv to this class, but it doesn't seem to pick it up. math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3 I can't see why that is, becuase my opMul works in the same place. Can anyone point out what I have done wrong? Class Matrix { void normalise() { const float mag = magnitude(); if (mag) { //this.scalarDivide(mag); this /= mag; // Not work this *= 1/mag; // Does work. } } // Vector3 opBinary(string op)(Vector3 rhs) if (op=='/') Vector3 opDiv(float scalar) { Vector3 v = this; v.scalarDivide(scalar); return v; } }
Aug 30 2015
On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:I have just added an opDiv to this class, but it doesn't seem to pick it up. math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3 I can't see why that is, becuase my opMul works in the same place. Can anyone point out what I have done wrong? Class Matrix { void normalise() { const float mag = magnitude(); if (mag) { //this.scalarDivide(mag); this /= mag; // Not work this *= 1/mag; // Does work. } } // Vector3 opBinary(string op)(Vector3 rhs) if (op=='/') Vector3 opDiv(float scalar) { Vector3 v = this; v.scalarDivide(scalar); return v; } }Don't use opMul or opDiv etc, use opBinary Could you provide a more complete example? It's hard to identify what the problem is from this snippet.
Aug 30 2015
On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:I have just added an opDiv to this class, but it doesn't seem to pick it up. math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3 I can't see why that is, becuase my opMul works in the same place. Can anyone point out what I have done wrong? Class Matrix { void normalise() { const float mag = magnitude(); if (mag) { //this.scalarDivide(mag); this /= mag; // Not work this *= 1/mag; // Does work. } } // Vector3 opBinary(string op)(Vector3 rhs) if (op=='/') Vector3 opDiv(float scalar) { Vector3 v = this; v.scalarDivide(scalar); return v; } }try --- Vector3 opBinary(string op)(Vector3 rhs) { static if (op =="/"){} else static assert(0, op ~ " not implemented"); } --- you used the char litteral delims ('') instead of the strings ones ("").
Aug 30 2015
On Sunday, 30 August 2015 at 18:12:40 UTC, BBasile wrote:On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:Ah yes, it's didnt' complain. But anyway that was the wrong funciton I implemented. I found the problem. I failed to implement a opDivAssign[...]try --- Vector3 opBinary(string op)(Vector3 rhs) { static if (op =="/"){} else static assert(0, op ~ " not implemented"); } --- you used the char litteral delims ('') instead of the strings ones ("").
Aug 30 2015
On 8/30/15 3:45 PM, Spacen Jasset wrote:On Sunday, 30 August 2015 at 18:12:40 UTC, BBasile wrote:Actually, that sounds like a bug to me. Trying to compare a string against a character should be an error, even in compile time constraint I would think.On Sunday, 30 August 2015 at 17:02:58 UTC, Spacen Jasset wrote:Ah yes, it's didnt' complain.[...]try --- Vector3 opBinary(string op)(Vector3 rhs) { static if (op =="/"){} else static assert(0, op ~ " not implemented"); } --- you used the char litteral delims ('') instead of the strings ones ("").But anyway that was the wrong funciton I implemented.Yes, but still you should use opBinary, even though the D1 versions will likely exist forever. It can tremendously cut down on your code size, and reduce your exposure to simple copy/paste bugs. -Steve
Aug 31 2015
On 08/30/2015 07:02 PM, Spacen Jasset wrote:I have just added an opDiv to this class, but it doesn't seem to pick it up. math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3 I can't see why that is, becuase my opMul works in the same place. Can anyone point out what I have done wrong? ...import std.math: sqrt; import std.algorithm: map,sum,canFind; struct Vector3{ float[3] xyz; void normalise(){ this/=magnitude(); } float magnitude(){ return sqrt(xyz[].map!(a=>a*a).sum); } enum scalarOps=["*","/"]; enum isScalarOp(string op)=scalarOps.canFind(op); void scalar(string op)(float scalar)if(isScalarOp!op){ foreach(ref a;xyz) mixin(`a `~op~`=scalar;`); } Vector3 opBinary(string op)(float scalar)if(isScalarOp!op){ Vector3 v=this; v.scalar!op(scalar); return v; } auto opOpAssign(string op)(float rhs)if(isScalarOp!op){ return mixin(`this=this `~op~` rhs`); } }
Aug 30 2015
On Sunday, 30 August 2015 at 20:09:25 UTC, Timon Gehr wrote:On 08/30/2015 07:02 PM, Spacen Jasset wrote:Thanks, that is interesting. I am curently porting, rather than trying to rewrite anything at the moment, but I will try this out later.[...]import std.math: sqrt; import std.algorithm: map,sum,canFind; struct Vector3{ float[3] xyz; void normalise(){ this/=magnitude(); } float magnitude(){ return sqrt(xyz[].map!(a=>a*a).sum); } enum scalarOps=["*","/"]; enum isScalarOp(string op)=scalarOps.canFind(op); void scalar(string op)(float scalar)if(isScalarOp!op){ foreach(ref a;xyz) mixin(`a `~op~`=scalar;`); } Vector3 opBinary(string op)(float scalar)if(isScalarOp!op){ Vector3 v=this; v.scalar!op(scalar); return v; } auto opOpAssign(string op)(float rhs)if(isScalarOp!op){ return mixin(`this=this `~op~` rhs`); } }
Aug 30 2015