整数除法
根据定义,整数除法返回商。
为什么 4613.9145 div 100.
给出错误(“错误的参数”)?
By definition the integer division returns the quotient.
Why 4613.9145 div 100.
gives an error ("bad argument") ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于
div
,参数必须是整数。/
接受任意数字作为参数,尤其是浮点数。因此,对于您的示例,以下内容将有效:要对比差异,请尝试:
文档: http: //www.erlang.org/doc/reference_manual/expressions.html
更新:整数除法,有时表示为
\
,可以定义为:因此,您需要一个下限函数,该函数不在标准库中。
用法:
For
div
the arguments need to be integers./
accepts arbitrary numbers as arguments, especially floats. So for your example, the following would work:To contrast the difference, try:
Documentation: http://www.erlang.org/doc/reference_manual/expressions.html
Update: Integer division, sometimes denoted
\
, can be defined as:So you'll need a floor function, which isn't in the standard lib.
Usage:
Erlang 中的整数除法
div
被定义为以两个整数作为输入并返回一个整数。您在之前的评论中给出的链接,http://mathworld.wolfram.com/IntegerDivision.html,在其示例中仅使用整数,因此在本次讨论中并没有多大用处。使用trunc
和round
将允许您使用您想要的任何参数。Integer division in Erlang,
div
, is defined to take two integers as input and return an integer. The link you give in an earlier comment, http://mathworld.wolfram.com/IntegerDivision.html, only uses integers in its examples so is not really useful in this discussion. Usingtrunc
andround
will allow you use any arguments you wish.我不太明白你所说的“定义”是什么意思。语言设计者可以按照自己的意愿自由定义运算符。在 Erlang 中,他们定义了 div 来只接受整数参数。
如果您有兴趣了解 Erlang 创建者的设计决策,可以给他们发送电子邮件。另外,如果您有足够的好奇心来筛选(非常短的)语法,您可以找到它此处。祝你好运!
I don't know quite what you mean by "definition." Language designers are free to define operators however they wish. In Erlang, they have defined
div
to accept only integer arguments.If it is the design decisions of Erlang's creators that you are interested in knowing, you could email them. Also, if you are curious enough to sift through the (remarkably short) grammar, you can find it here. Best luck!
不确定您在寻找什么,@Bertaud。不管它在其他地方如何定义,Erlang 的
div
只适用于整数。您可以在调用div
之前将参数转换为整数:将商转换为整数:
或者您可以使用
/
代替div
,然后trunc
可能是你想要的,也可能不是——你可能想要round
,或者地板或天花板(这在 Erlang 的标准库中没有定义,但定义起来并不难你自己,就像 miku 对上面的floor
所做的那样)。这就是 Erlang 不做任何假设并为你进行转换的部分原因。但无论如何,如果你想要 Erlang 中两个非整数的整数商,你必须在某个地方有某种显式转换步骤。Not sure what you're looking for, @Bertaud. Regardless of how it's defined elsewhere, Erlang's
div
only works on integers. You can convert the arguments to integers before callingdiv
:or you can use
/
instead ofdiv
and convert the quotient to an integer afterward:And
trunc
may or may not be what you want- you may wantround
, or floor or ceiling (which are not defined in Erlang's standard library, but aren't hard to define yourself, as miku did withfloor
above). That's part of the reason Erlang doesn't assume something and do the conversion for you. But in any case, if you want an integer quotient from two non-integers in Erlang, you have to have some sort of explicit conversion step somewhere.