Multiplication of decimal and integer F #

I am learning f #. Given this function:

let mult (a:decimal) (b:int) : decimal = a * b

When I try to compile, I get this error: Error 1 Type restriction mismatch. Int is not compatible with decimal

Why does the compiler not accept this?

PS: When I convert the expression, it compiles:

let mult (a:decimal) (b:int) : decimal = a * decimal(b)
+4
source share
2 answers

F # is very strict with numeric types. There are several reasons for this.

  • Accuracy - 4.0 * 8.5cannot be exactly equal 4*8.5due to loss of accuracy when converting to a floating point. Forcing you to convert 4to 4.0, the language made the even loss transparent
  • . . int float, , a

    let test a = a + 1.0
    

    int, float a. ( , , genericZero genericOne

  • . , , .
+4

. , . , , , , .

+3

Source: https://habr.com/ru/post/1538135/


All Articles