OCaml 中的一元减号和浮点数 [英] Unary minus and floating point number in OCaml

查看:15
本文介绍了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.|]

这里pt是一个float类型的函数 ->浮动 ->复杂.t.但是ocaml 拒绝编译这个说法:

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

我在这里想做的是(显然)包括实部为 -4 虚部为 1 的复数.但是 ocaml 将我打算成为一元减号的内容视为int 类型的函数 ->int -> 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?

推荐答案

这是我的想法(在阅读文档和实验之后).确实有四种完全不同的运算符:

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

如果每个人都使用这些运算符,事情就很清楚了,但不幸的是,它也是一个非常笨拙的符号.根据我的经验,~-~-. 运算符很少使用.OCaml 语法被指定为允许您将减法运算符用作一元否定运算符,就像在许多其他语言中一样.如果这样做,则通常必须使用额外的括号.如果您愿意使用特定的一元运算符,则不需要括号.

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.

即,您可以写(如 pad 的编辑答案):

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.|]

或者你可以写:

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

还有一个额外的令人困惑的因素,那就是 OCaml 词法分析器被指定为让您在将 integer 减法运算符与浮动 常量.同样,这使得符号更像其他语言.因为它本质上是一个二元运算符,所以这里也需要括号.

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.

这意味着你可以写:

[|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.

这篇关于OCaml 中的一元减号和浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆