Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unary minus and floating point number in OCaml

Tags:

syntax

ocaml

I wanted to have a vector of complex numbers in my program, so I wrote this:

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

Here pt is a function of type float -> float -> Complex.t. But ocaml refused to compile this saying:

Characters 12-14:
  [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|];;
              ^^
Error: This expression has type float -> float -> Complex.t
       but an expression was expected of type int

What I wanted to do here is (obviously) include the complex number whose real part is -4 and whose imaginary part is 1. But ocaml treated what I intended to be an unary minus as a function of type int -> int ->int.

What should I write to do what I wanted to?

like image 501
Pteromys Avatar asked Jan 24 '12 09:01

Pteromys


1 Answers

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.
like image 200
Jeffrey Scofield Avatar answered Dec 03 '22 00:12

Jeffrey Scofield