Unary minus and floating point number in OCaml

Here’s how I think it goes (after reading docs and experimenting). There really are four completely different operators:

-    Integer subtraction        int -> int -> int
-.   Floating subtraction       float -> float -> float
~-   Integer unary negation     int -> int
~-.  Floating unary negation    float -> float

If everybody used these operators, things would be clear, but unfortunately it’s also a pretty clumsy notation. In my experience the ~- and ~-. operators are rarely used. The OCaml grammar is specified to let you use the subtraction operators as unary negation operators, as in many other languages. If you do that, you often have to use extra parentheses. If you’re willing to use the specific unary operators, you don’t need the parentheses.

I.e., you can write (as in pad’s edited answer):

[|pt 0. 0.; pt ~-.4. 1.; pt ~-.7. ~-.2.; pt 4. 5.; pt 1. 1.|]

Or you can write:

[|pt 0. 0.; pt (-.4.) 1.; pt (-.7.) (-.2.); pt 4. 5.; pt 1. 1.|]

There’s also one extra confusing factor, which is that the OCaml lexer is specified to let you use the integer subtraction operator for unary negation when you use it with a floating constant. Again, this makes the notation more like other languages. Since it’s fundamentally a binary operator, you need the parentheses here too.

This means you can write:

[|pt 0. 0.; pt (-4.) 1.; pt (-7.) (-2.); pt 4. 5.; pt 1. 1.|]

This notation only works for negative floating constants. The other two notations work for any expression you might want to negate.

# (-) ;;
- : int -> int -> int = <fun>
# (-.) ;;
- : float -> float -> float = <fun>
# (~-) ;;
- : int -> int = <fun>
# (~-.) ;;
- : float -> float = <fun>

# let f x = x +. 2.0;;
val f : float -> float = <fun>

# f ~-.5.;;
- : float = -3.

# f -.5.;;
Characters 0-1:
  f -.5.;;
  ^
Error: This expression has type float -> float
       but an expression was expected of type float
# f (-.5.);;
- : float = -3.

# f -5.;;
  ^
Error: This expression has type float -> float
       but an expression was expected of type int
# f (-5.);;
- : float = -3.

Leave a Comment