在 Erlang 中如何求模或求余?

发布于 2024-07-10 04:36:07 字数 161 浏览 7 评论 0原文

我是 Erlang 的新手。 如何进行取模(得到除法的余数)? 在大多数类似 C 的语言中它是 %,但在 Erlang 中它指定注释。

有几个人回答了 rem,大多数情况下都可以。 但我正在重新审视这一点,因为现在我需要使用负数,而 rem 给出除法的余数,这与负数的模不同。

I'm brand new to Erlang. How do you do modulo (get the remainder of a division)? It's % in most C-like languages, but that designates a comment in Erlang.

Several people answered with rem, which in most cases is fine. But I'm revisiting this because now I need to use negative numbers and rem gives you the remainder of a division, which is not the same as modulo for negative numbers.

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

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

发布评论

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

评论(8

行至春深 2024-07-17 04:36:07

在 Erlang 中,5 rem 3. 给出 2-5 rem 3. 给出 -2。 如果我理解你的问题,你会希望 -5 rem 3. 给出 1,因为 -5 = -2 * 3 + 1.

这是否符合你的要求?

mod(X,Y) when X > 0 -> X rem Y;
mod(X,Y) when X < 0 -> Y + X rem Y;
mod(0,Y) -> 0.

In Erlang, 5 rem 3. gives 2, and -5 rem 3. gives -2. If I understand your question, you would want -5 rem 3. to give 1 instead, since -5 = -2 * 3 + 1.

Does this do what you want?

mod(X,Y) when X > 0 -> X rem Y;
mod(X,Y) when X < 0 -> Y + X rem Y;
mod(0,Y) -> 0.
无人问我粥可暖 2024-07-17 04:36:07

erlang 模运算符是 rem

Eshell V5.6.4  (abort with ^G)
1> 97 rem 10.
7

The erlang modulo operator is rem

Eshell V5.6.4  (abort with ^G)
1> 97 rem 10.
7
嘿嘿嘿 2024-07-17 04:36:07

我在 Elixir 中使用了以下内容:

defp mod(x,y) when x > 0, do: rem(x, y);
defp mod(x,y) when x < 0, do: rem(x, y) + y;
defp mod(0,_y), do: 0

I used the following in elixir:

defp mod(x,y) when x > 0, do: rem(x, y);
defp mod(x,y) when x < 0, do: rem(x, y) + y;
defp mod(0,_y), do: 0
回忆凄美了谁 2024-07-17 04:36:07

根据这篇博文,它是<代码>雷姆。

According to this blog post, it's rem.

冷夜 2024-07-17 04:36:07

上面的 Y + X rem Y 似乎是错误的:(Y + X) rem Y 或 Y + (X rem Y) 会产生错误的结果。 例如:设 Y=3。 如果 X=-4,第一种形式返回 -1,如果 X=-3,第二种形式返回 3,这些都不在 [0;3[ 中。

我用这个代替:

% Returns the positive remainder of the division of X by Y, in [0;Y[. 
% In Erlang, -5 rem 3 is -2, whereas this function will return 1,  
% since -5 =-2 * 3 + 1.

modulo(X,Y) when X > 0 ->   
   X rem Y;

modulo(X,Y) when X < 0 ->   
    K = (-X div Y)+1,
    PositiveX = X + K*Y,
    PositiveX rem Y;

modulo(0,_Y) -> 
    0.

The above Y + X rem Y seems to be wrong: either (Y + X) rem Y or Y + (X rem Y) yield incorrect results. Ex: let Y=3. If X=-4, the first form returns -1, if X=-3 the second form returns 3, none of which is in [0;3[.

I use this instead:

% Returns the positive remainder of the division of X by Y, in [0;Y[. 
% In Erlang, -5 rem 3 is -2, whereas this function will return 1,  
% since -5 =-2 * 3 + 1.

modulo(X,Y) when X > 0 ->   
   X rem Y;

modulo(X,Y) when X < 0 ->   
    K = (-X div Y)+1,
    PositiveX = X + K*Y,
    PositiveX rem Y;

modulo(0,_Y) -> 
    0.
玩物 2024-07-17 04:36:07

Erlang 余数不适用于负数,因此您必须为负参数编写自己的函数。

Erlang remainder not works with negative numbers, so you have to write your own function for negative parameters.

吹梦到西洲 2024-07-17 04:36:07

接受的答案是错误的。

rem 的行为与现代 C 中的 % 运算符完全相同。它使用截断除法。

对于 X<0 和 Y<0,接受的答案失败。 考虑 mod(-5,-3):

C:                     -5 % -3 == -2
rem:                 -5 rem -3 == -2
Y + X rem Y:    -3 + -5 rem -3 == -5 !! wrong !!

模运算符的替代实现使用向下除法和欧几里得除法。 这些结果是

flooring division:   -5 mod -3 == -2
euclidean division:  -5 mod -3 == 1

所以

Y + X rem Y

不会为 X < 重现任何模运算符。 0且Y<1 0.

并且rem按预期工作——它使用截断除法。

The accepted answer is wrong.

rem behaves exactly like the % operator in modern C. It uses truncated division.

The accepted answer fails for X<0 and Y<0. Consider mod(-5,-3):

C:                     -5 % -3 == -2
rem:                 -5 rem -3 == -2
Y + X rem Y:    -3 + -5 rem -3 == -5 !! wrong !!

The alternative implementations for the modulo operator use floored division and Euclidean division. The results for those are

flooring division:   -5 mod -3 == -2
euclidean division:  -5 mod -3 == 1

So

Y + X rem Y

doesn't reproduce any modulo operator for X < 0 and Y < 0.

And rem works as expected -- it's using truncated division.

清风挽心 2024-07-17 04:36:07
mod(A, B) when A > 0 -> A rem B;
mod(A, B) when A < 0 -> mod(A+B, B); 
mod(0, _) -> 0.

% console:
3> my:mod(-13, 5).
2
mod(A, B) when A > 0 -> A rem B;
mod(A, B) when A < 0 -> mod(A+B, B); 
mod(0, _) -> 0.

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