除法、余数和仅允许实数
试图找出这个伪代码。假设以下情况...... 我只能使用无符号和有符号整数(或长整型)。 除法返回一个没有余数的实数。 MOD 返回一个实数。 不处理分数和小数。
INT I = 41828;
INT C = 15;
INT D = 0;
D = (I / 65535) * C;
在这种情况下,您将如何处理分数(或小数值)?有没有办法用负值来表示余数?
在这个例子中,I/65535 应该是 0.638,但是,由于限制,我得到 0,MOD 为 638。然后我如何乘以 C 才能得到正确的答案?
希望这是有道理的。
这里的 MOD 实际上会返回 23707,而不是 638。(我希望我是对的:))
Trying to figure out this pseudo code. The following is assumed....
I can only use unsigned and signed integers (or long).
Division returns a real number with no remainder.
MOD returns a real number.
Fractions and decimals are not handled.
INT I = 41828;
INT C = 15;
INT D = 0;
D = (I / 65535) * C;
How would you handle a fraction (or decimal value) in this situation? Is there a way to use negative value to represent the remainder?
In this example I/65535 should be 0.638, however, with the limitations, I get 0 with a MOD of 638. How can I then multiply by C to get the correct answer?
Hope that makes sense.
MOD here would actually return 23707, not 638. (I hope I'm right on that :) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要在最后一行上切换操作顺序,您将得到您正在寻找的整数答案(9,如果我的计算是正确的)
这是您正在寻找的答案吗?
If you were to switch your order of operations on that last line, you would get the integer answer you're looking for (9, if my calculations are correct)
Is that the answer you're looking for?
处理小数的一种方法是替换除法函数。这种技术有许多明显的缺点。
Well, one way to handle decimals is this replacement division function. There are numerous obvious downsides to this technique.
假设这些是您始终用于此计算的值,那么我会执行以下操作:
或者
因为 C 是 65535 的因数。这将有助于减少超出整数可用范围的可能性(即,如果您只有 16 位无符号整数)。
在更一般的情况下,如果您认为 I 和 C 的乘法存在导致值超出您所使用的整数类型允许范围的风险(即使最终结果在该范围内),您可以分解出分子和分母的 GCD 如下:
其中 DEN 是你的分母 (65535在这种情况下)。这不会在所有情况下为您提供正确的答案,特别是当 I 和 C 都互质于 DEN 且 I*C > 时。 MAX_INT。
至于您提出的更大问题,整数值的除法总是会丢失小数部分(相当于下限函数)。保留我们所认为的“小数”部分中包含的信息的唯一方法是通过可以从模数导出的余数。我强烈建议您不要混淆这些不同数字系统的含义。整数就是整数。如果您需要它们是浮点数,那么您应该使用浮点数,而不是整数。如果您感兴趣的只是向用户显示小数部分(即您并未真正将其用于进一步计算),那么您可以编写一个例程将余数转换为表示余数的字符串。
Assuming these are the values you're always using for this computation, then I would do something like:
or
Since C is a factor of 65535. This will help to reduce the possibility of overruning the available range of integers (i.e. if you've only got 16 bit unsigned ints).
In the more general case you, if you think there's a risk that the multiplication of I and C will result in a value outside the allowed range of the integer type you're using (even if the final result will be inside that range) you can factor out the GCD of the numerator and denominator as in:
Where DEN is your denominator (65535 in this case). This will not provide you with the correct answer in all cases, especially if I and C are both mutually prime to DEN and I*C > MAX_INT.
As to the larger question you raise, division of integer values will always loose the decimal component (equivalent to the floor function). The only way to preserve the information contained in what we think of as the "decimal" part is through the remainder which can be derived from the modulus. I highly encourage you to not mix the meanings of these different number systems. Integers are just that integers. If you need them to be floating point numbers, you should really be using floats, not ints. If all you're interested in doing is displaying the decimal part to the user (i.e. you're not really using it for further computation) then you could write a routine to convert the remainder into a character string representing the remainder.