Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the exponential operator use float variables in OCaml?

Tags:

ocaml

Why does the exponential operator use float variables in OCaml? Shouldn't it allow int variables too?

# 3**3;;
Error: This expression has type int but an expression was expected of type
         float

Works:

# 3.0**3.0;;
- : float = 27.
like image 884
SadSeven Avatar asked Jul 02 '26 21:07

SadSeven


1 Answers

So, the existing answers go into how to get around this, but not into why it is the case. There are two main reasons:

1) OCaml doesn't have operator aliasing. You can't have two operators that do the "same thing", but to different types. This means that only one kind of number, integers or floats (or some other representation) will get to use the standard ** interface.

2) pow(), the exponentiation function has historically been defined on floats (for instance, in Standard C).

Also, for another way to get around the problem, if you're using OCaml Batteries included, there is a pow function defined for integers.

like image 65
slongfield Avatar answered Jul 05 '26 16:07

slongfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!