Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error FS0001: The type 'int' does not match the type 'float'

Tags:

f#

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 ?

like image 778
fahadash Avatar asked Oct 29 '25 19:10

fahadash


1 Answers

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.

like image 123
Tomas Petricek Avatar answered Oct 31 '25 13:10

Tomas Petricek