Modulus in Java

ぃ、小莉子 提交于 2019-12-13 01:15:40

问题


I want to get the value of an unknown number in equation containing modulus % in Java

For example:

x % 26 = y if I have the value of y how can I get x


回答1:


The problem is that there are either zero solutions (if Math.abs(y) >= 26) or an infinite1 number of values of x that satisfy that equation for a given y. The general answer is:

x = 26 * k + y

for any integer value of k. You can pick whatever k you want.2

1 In practice, the range will be limited by the range of integer values you are using. If x and y are int values, then you are limited by Integer.MAX_VALUE and Integer.MIN_VALUE. On the other hand, if they are BigInteger values, you don't have much in the way of range constraints.

2 Actually, the signs of x and y must be the same in Java, so you only have half of infinity to pick from. :-)




回答2:


You can't get the value of x, that's how modulus works. You just know x = 26 * k + y where k is an integer.



来源:https://stackoverflow.com/questions/16237319/modulus-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!