gpt4 book ai didi

c# - 模数运算如何与 float 数据类型一起使用?

转载 作者:太空狗 更新时间:2023-10-29 20:58:42 25 4
gpt4 key购买 nike

我正在尝试找出对 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 的情况下,如果 xy 是正有限值。

z is the result of x % y and is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com