为什么是“**”? 在 OCaml 中,它的结合不比否定更紧密吗?

发布于 2024-07-23 02:00:46 字数 395 浏览 7 评论 0原文

这个问题之后,我不知道该怎么想。

在 OCaml 中,如果您执行类似 -1.0**2.0 的操作(因为您需要浮动类型),您将获得 1.00。 根据标准操作顺序,结果应该是-1(如在Python中)。

我无法在 OCaml 中找到原因或运算符优先级的明确定义...

这是因为类型系统吗? 或者事实上下面有一个带有 pow 的绑定?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱纠缠 2024-07-30 02:00:46

正如您引用的那一页所说,“一元运算符 - (通常读作“减”)的操作顺序通常是有问题的。” -- 它引用了 Excel 和 bc,认为其与 O'CAML 具有相同的优先级,但也表示“在书面或印刷数学中”它的工作方式与 Python 中相同。 因此,从本质上讲,在这个具体问题上还没有达成普遍共识。

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.

神也荒唐 2024-07-30 02:00:46

在 OCaml 中,运算符优先级是语法决定的,这意味着函数标识符的第一个字符(无论是一元还是二进制)根据固定顺序确定运算符优先级。 将此与 Haskell 等语言进行对比,其中可以在函数定义中指定运算符优先级,而不管使用哪些字符来形成函数标识符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文