I have a simple temperature converter class that I am trying to work on.
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"
I am getting the following compile errors
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'
What am I missing? What part of the code it is inferring as int ?
F# does not automatically convert integers to floats, so you need:
type Converter() =
member this.FtoC (f : float) = (5.0/9.0) * (f - 32.0)
member this.CtoF(c : float) = (9.0/5.0) * c + 32.0
In your original code 5/9 is of type int and f-32.0 is of type float. Numerical operators like * require both arguments to be of the same type, so you get an error. In the fixed version, I used 5.0/9.0, which is of type float (because it uses floating-point numerical literals), and so the compiler is happy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With