tags:

views:

194

answers:

2

after this question, I don't know what to think.

In OCaml, if you do something like -1.0**2.0 (because of the typing you need to have float), you obtain 1.00. According to the standard order of operations, the result should be -1 (as in python).

I wasn't able to find the reason or a clear definition of the operator precedence in OCaml...

Is this because of the type system ? or the fact that there's a binding underneath with pow ?

+3  A: 

As the very page you quote says, "The order in which the unary operator − (usually read "minus") acts is often problematical." -- it quotes Excel and bc as having the same priority for it as O'CAML, but also says "In written or printed mathematics" it works as in Python. So, essentially, there's no universal consensus on this specific issue.

Alex Martelli
+2  A: 

Operator precedence is syntax-directed in OCaml, which means that the first character of the function identifier (and whether it's unary or binary) determines the operator precedence according to a fixed sequence. Contrast this with languages like Haskell, where the operator precedence can be specified at function definition regardless of which characters are used to form the function identifier.

james woodyatt
This is not always correct and irrelevant to the question. For example, ** has higher precedence than *, even though they start with the same character; and unary minus has higher precedence than subtraction, even though they use the same character. In fact, the OP's problem is that unary minus is in fact higher precedence than both multiplication and exponentiation in OCaml; which could not be arrived at if you assumed that unary minus has the same precedence as subtraction.
newacct
Section 6.7 of the OCaml manual has the table of operator precedences. All the binary operators have lower precedence than the unary operators (excepting the record, array and string projection binary operators). To get the standard order of arithmetic operations, and still allow programmers to define their own operators, OCaml would need some additional language features that it doesn't have, and operators would not be syntax-directed anymore.
james woodyatt