OCaml中的一元减号和浮点数

编程入门 行业动态 更新时间:2024-10-13 12:17:02
本文介绍了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中的一元减号和浮点数

本文发布于:2023-11-29 12:14:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:减号   浮点数   OCaml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!