2
我有一个简单的温度转换器类,我正在努力。错误FS0001:类型'int'与'float'类型不匹配
open System
type Converter() =
member this.FtoC (f : float) = (5/9) * (f - 32.0)
member this.CtoF(c : float) = (9/5) * c + 32.0
let conv = Converter()
54.0 |> conv.FtoC |> printfn "54 F to C: %A"
32.0 |> conv.CtoF |> printfn "32 C to F: %A"
我正在以下编译错误
prog.fs(4,46): error FS0001: The type 'float' does not match the type 'int'
prog.fs(4,39): error FS0043: The type 'float' does not match the type 'int'
我缺少什么?它推断为int的代码是什么部分?
即使两个整数的划分不是推断为单个或浮点? – fahadash
不,它是一个整数除法“9/5 = 1”,而“9.0/5.0 = 1.8”。事实上,使用整数除法,然后将结果转换为浮点数将是一个错误,因为您会得到1而不是1.8! –