Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net F#重载运算符(*=),带浮点数,不与其他浮点数一起使用_.net_F#_Operator Overloading - Fatal编程技术网

.net F#重载运算符(*=),带浮点数,不与其他浮点数一起使用

.net F#重载运算符(*=),带浮点数,不与其他浮点数一起使用,.net,f#,operator-overloading,.net,F#,Operator Overloading,我正在规范化我的向量,就像这个函数: 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 因此,我在F#代码文件中重载了

我正在规范化我的向量,就像这个函数:

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
因此,我在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
操作员通过了Vector*=VectorVector*=Float的测试,但当我尝试执行Float*=Float

第三个测试函数-float*=float:完全失败,出现完全相同的错误

member this.MultiplyAssignmentTest_3(v1:float,v2:float):float = v1 *= v2

OFC我总是可以编写
this.X,正如John所说,最好避免使用可变项。
另一个问题是,您希望为float重载运算符(*=),但您没有float类型的定义,因此您试图在类的定义中这样做,并且重载将永远不会执行,因为没有属于类(vector2)类型的参数

其他两个重载工作正常,因为重载中至少有一个参数的类型为(vector2),因此它们将解析

您可以在全局级别定义(=*),但是它不能与vector2一起使用,所以您需要在全局级别的定义中指定所有重载

您可以尝试以下代码,该代码使用独立类进行重载:

type vector2 = {x:float; y:float} with

// Operator
    static member ( *= ) ( v1:vector2, v2:vector2 ) =
       {x = v1.x + v1.x * v2.x; y = v1.y + v1.y * v2.y }

    static member ( *= ) ( v1:vector2, v2:float )  = 
       {x = v1.x + v1.x * v2; y = v1.y + v1.y * v2 }

type Overloads = Overloads with
    static member (?<-) (a:vector2, Overloads, b:vector2) = a *= b
    static member (?<-) (a:vector2, Overloads, b:float)   = a *= b
    static member (?<-) (a:float  , Overloads, b:float)   = a *  b

let inline ( *= ) f1 f2 =  f1 ? (Overloads) <- f2

// now you can add more methods for vector2 using the global definition of ( *=)
type vector2 with 
    static member Length (v1:vector2) = sqrt( v1.x ** 2.0 + v1.y ** 2.0)
    // more methods
type vector2={x:float;y:float}带
//接线员
静态成员(*=)(v1:vector2,v2:vector2)=
{x=v1.x+v1.x*v2.x;y=v1.y+v1.y*v2.y}
静态成员(*=)(v1:vector2,v2:float)=
{x=v1.x+v1.x*v2;y=v1.y+v1.y*v2}
类型重载=带有

静态成员(?正如John所说,最好避免使用可变项。 另一个问题是,您希望为float重载运算符(*=),但您没有float类型的定义,因此您试图在类的定义中这样做,并且重载将永远不会执行,因为没有属于类(vector2)类型的参数

其他两个重载工作正常,因为重载中至少有一个参数的类型为(vector2),因此它们将解析

您可以在全局级别定义(=*),但是它不能与vector2一起使用,所以您需要在全局级别的定义中指定所有重载

您可以尝试以下代码,该代码使用独立类进行重载:

type vector2 = {x:float; y:float} with

// Operator
    static member ( *= ) ( v1:vector2, v2:vector2 ) =
       {x = v1.x + v1.x * v2.x; y = v1.y + v1.y * v2.y }

    static member ( *= ) ( v1:vector2, v2:float )  = 
       {x = v1.x + v1.x * v2; y = v1.y + v1.y * v2 }

type Overloads = Overloads with
    static member (?<-) (a:vector2, Overloads, b:vector2) = a *= b
    static member (?<-) (a:vector2, Overloads, b:float)   = a *= b
    static member (?<-) (a:float  , Overloads, b:float)   = a *  b

let inline ( *= ) f1 f2 =  f1 ? (Overloads) <- f2

// now you can add more methods for vector2 using the global definition of ( *=)
type vector2 with 
    static member Length (v1:vector2) = sqrt( v1.x ** 2.0 + v1.y ** 2.0)
    // more methods
type vector2={x:float;y:float}带
//接线员
静态成员(*=)(v1:vector2,v2:vector2)=
{x=v1.x+v1.x*v2.x;y=v1.y+v1.y*v2.y}
静态成员(*=)(v1:vector2,v2:float)=
{x=v1.x+v1.x*v2;y=v1.y+v1.y*v2}
类型重载=带有
静态构件(?这也适用于:

let inline ( *= ) (p : byref<_>) x = p <- p * x

[<Literal>]
let _zerotolerance = 1e-3

type A(x0, y0) =
    let mutable x = x0
    let mutable y = y0

    member __.Length = sqrt (x * x + y * y)

    member this.Normalize =
        if this.Length > _zerotolerance  then
            let inv:float = 1.0 / this.Length 
            &x *= inv
            &y *= inv
let inline(*=)(p:byref)x=p\u
让inv:float=1.0/此长度
&x*=库存
&y*=inv
这也适用于:

let inline ( *= ) (p : byref<_>) x = p <- p * x

[<Literal>]
let _zerotolerance = 1e-3

type A(x0, y0) =
    let mutable x = x0
    let mutable y = y0

    member __.Length = sqrt (x * x + y * y)

    member this.Normalize =
        if this.Length > _zerotolerance  then
            let inv:float = 1.0 / this.Length 
            &x *= inv
            &y *= inv
let inline(*=)(p:byref)x=p\u
让inv:float=1.0/此长度
&x*=库存
&y*=inv

对于一个开始,类型签名将是错误的,就像
f1
是一个
float
你不能分配给它,因为
float
是不可更改的。对于一个开始,类型签名将是错误的,就像
f1
是一个
float
你不能分配给它,因为
float
是不可更改的。看起来不错。我正在去的路上我的第一个F#项目(包括整个数学库)是从VB.Net完全移植Vector2类(其他人可能也会这么做),这些都是我的重载、内置翻译函数/例程,这些都是一个单独的类,同时还利用了一个共享的数学库。我担心我会失去一些精度(我需要很多精度)。谢谢。好的。看起来不错。我正在从VB.Net(其他人可能会跟随)完全移植Vector2类,作为我的第一个F#项目(包括整个数学库),都是我的重载,内置的翻译函数/例程,这些都是一个单独的类,同时也利用了一个共享的数学库。我担心我会失去一些精度(我需要很多精度)。谢谢。
let inline ( *= ) (p : byref<_>) x = p <- p * x

[<Literal>]
let _zerotolerance = 1e-3

type A(x0, y0) =
    let mutable x = x0
    let mutable y = y0

    member __.Length = sqrt (x * x + y * y)

    member this.Normalize =
        if this.Length > _zerotolerance  then
            let inv:float = 1.0 / this.Length 
            &x *= inv
            &y *= inv