我正在像这个函数一样规范化我的向量:
member this.Normalize =
if this.Length > _zerotolerance then
let inv:float = 1.0 / this.Length
this.x <- this.x *= inv // overloaded
this.y <- this.y *= inv // overloaded
但是 -默认情况下,我无法使用用于浮点数的乘法赋值运算符 *=,因此我需要创建一个。
根据 MSDN http://msdn.microsoft.com/en-us/library/dd233204%28v=vs.110%29我必须以下列方式重载运算符:
static member ( *= ) (arg1 : type, arg1: type) = ...code etc...
因此,我在我的 F# 代码文件中重载了以下用于乘法赋值的运算符,如下所示:
// Operator
static member ( *= ) ( v1:vector2, v2:vector2 ) =
v1.x <- v1.x + v1.x * v2.x
v1.y <- v1.y + v1.y * v2.y
static member ( *= ) ( v1:vector2, v2:float ) =
v1.x <- v1.x + v1.x * v2
v1.y <- v1.y + v1.y * v2
// Operator
static member ( *= ) ( f1:float, f2:float ) =
f1 <- f1 + f1 * f2
它似乎不起作用。即使在重载运算符后,我也有以下问题:
vector2.fs(107,36):错误 FS0001:“浮点”类型不支持任何名为“*=”的运算符
所以运算符 *= 不适用于浮点数。它在这里失败(规范化功能):
this.X <- this.X *= inv
为了检查运算符的其他重载,我临时为 Vector2s 创建了测试函数:
member this.MultiplyAssignmentTest_1(v1:vector2,v2:vector2):vector2 = v1 *= v2
member this.MultiplyAssignmentTest_2(v1:vector2,v2:float):vector2 = v1 *= v2
操作员通过了Vector *= Vector和Vector *= Float的测试, 但是当我尝试执行Float *= Float时仍然失败
第三个测试函数 - float *= float:完全失败并出现完全相同的错误。
member this.MultiplyAssignmentTest_3(v1:float,v2:float):float = v1 *= v2
OFC 我总是可以写this.X <- This.X + This.X *= inv
,这对于简短的公式来说很好。但是当我开始用它编写大量向量数学时,这并不是很经济——而且为了快速编码。
我搞砸了什么?我真的以某种方式搞砸了我的超载,还是有一个我不知道的错误?
为什么它适用于我的Vector2 *= Float 但不适用于Float *= Float?
或者我自己没有注意到一个错字/大小写错误?