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

查看:74
本文介绍了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 -> float -> Complex.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将我打算作为一元减号的函数视为.

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.

即,您可以编写(如在记事本的编辑答案中一样):

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天全站免登陆