我正在尝试找出对 float 数据类型的简单模数运算。
float a=3.14f;
float b=10f;
result=a%b;
我得到结果 = 3.14
另一个使用十进制数据类型的例子:
decimal p=10;
decimal q=40;
result=p%q;
得到答案=20。
我不明白模数是如何工作的?
最佳答案
来自关于浮点余数的 C# 语言规范。在 x % y
的情况下,如果 x
和 y
是正有限值。
z
is the result ofx % y
and is computed asx – n * y
, wheren
is the largest possible integer that is less than or equal tox / y
.
C# 语言规范还清楚地列出了如何处理非零有限值、零、无穷大和 NaN 的所有可能组合的情况的表格,这些情况可能出现在浮点值 x % y 中。
y value | +y –y +0 –0 +∞ –∞ NaN -----+---------------------------- x +x | +z +z NaN NaN x x NaN –x | –z –z NaN NaN –x –x NaN v +0 | +0 +0 NaN NaN +0 +0 NaN a –0 | –0 –0 NaN NaN –0 –0 NaN l +∞ | NaN NaN NaN NaN NaN NaN NaN u –∞ | NaN NaN NaN NaN NaN NaN NaN e NaN | NaN NaN NaN NaN NaN NaN NaN
关于c# - 模数运算如何与 float 数据类型一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671518/