How does modulus operation works with float data type?(模数运算如何与浮点数据类型一起使用?)
问题描述
我正在尝试找出浮点数据类型的简单模数运算.
float a=3.14f;浮动 b=10f;结果=a%b;
我得到的结果= 3.14
另一个使用十进制数据类型的例子:
十进制 p=10;十进制 q=40;结果=p%q;
得到答案=20.
我不明白模数是如何工作的?
来自关于浮点余数的 C# 语言规范.在 x % y
的情况下,如果 x
和 y
是正有限值.
z
是 x % y
的结果,计算为 x – n * y
,其中 n
是小于或等于 x/y
的最大可能整数.
C# 语言规范还清楚地概述了如何处理浮点值 x % y 可能出现的非零有限值、零、无穷大和 NaN 的所有可能组合的情况.
<块引用> y 值|+y –y +0 –0 +∞ –∞ NaN-----+----------------x +x |+z +z NaN NaN x x NaN-x |–z –z NaN NaN –x –x NaNv +0 |+0 +0 NaN NaN +0 +0 NaN一个 –0 |–0 –0 NaN NaN –0 –0 NaNl +∞ |南 南 南 南 南 南 南 南u –∞ |南 南 南 南 南 南 南 南e NaN |南 南 南 南 南 南 南 南
I m trying to find out a simple modulus operation on float data type.
float a=3.14f;
float b=10f;
result=a%b;
I m getting result= 3.14
Another example using decimal data types:
decimal p=10;
decimal q=40;
result=p%q;
getting answer=20.
I am not understanding how does modulus works?
From the C# language spec on floating point remainder. In the case of x % y
if x
and y
are positive finite values.
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
.
The C# language spec also clearly outlines the table of what to do with the cases of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s which can occur with floating point values of 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
这篇关于模数运算如何与浮点数据类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模数运算如何与浮点数据类型一起使用?


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01