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
这篇关于模数运算如何与浮点数据类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模数运算如何与浮点数据类型一起使用?


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01