I am starting with ocaml and tried:
float_of_int 8/2;;
I was expecting that it returns 4.0 because 8/2 is 4 but I get an error saying that:
Error: This expression has type float but an expression was expected of type
int
What am I missing here?
Your expression is parsed like this:
(float_of_int 8) / 2
So you're asking to divide a float using /, which applies to ints.
Function application (indicated in OCaml by putting two expressions side by side) has very high precedence, higher than all the binary infix operators. So you need to use parentheses.
It will work if you write:
float_of_int (8/2)
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