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.
zis the result ofx % yand is computed asx – n * y, wherenis 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
这篇关于模数运算如何与浮点数据类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模数运算如何与浮点数据类型一起使用?
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
