为什么ruby modulo与Java/other lang不同? [英] Why ruby modulo is different from java/other lang ?

查看:44
本文介绍了为什么ruby modulo与Java/other lang不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是来自Java背景,并且努力理解Ruby中的模运算.

i am basically coming from java background and struggling to understand the modulo operation in Ruby.

(5 % 3)     
(-5 % 3)    
(5 % -3)    
(-5 % -3) 

上面的Java操作产生了,2个-22个-2

The above operation in Java yields, 2 -2 2 -2

但是在Ruby中,相同的表达式产生21个-1-2.

But in Ruby, the same expression yields 2 1 -1 -2 .

在逻辑上,红宝石擅长于此吗?如何在Ruby中实现模块操作?如果将相同的操作定义为Web服务,则这两个服务如何匹配逻辑.

How logically ruby is good at this? How the module operation is implemented in Ruby ? If the same operation is defined as a web services, how both services can match the logic.

推荐答案

在Java中,取模运算的结果与除数的符号相同.

In Java, the result of the modulo operation has the same sign as the dividend.

在Ruby中,它与除数的符号相同.Ruby中的 remainder()与分红具有相同的符号.

In Ruby, it has the same sign as the divisor. remainder() in Ruby has the same sign as the dividend.

您可能还希望参考模运算.

这篇关于为什么ruby modulo与Java/other lang不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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