如何模操作的工作方式与浮点数据类型? [英] How does modulus operation works with float data type?

查看:134
本文介绍了如何模操作的工作方式与浮点数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米试图找出对浮点数据类型的简单模操作。

 浮起= 3.14f;
浮动B = 10F;
结果= x%A;
 

我米得到的结果= 3.14

使用十进制数据类型又如:

 十进制P = 10;
十进制Q = 40;
结果= P%Q;
 

得到的答案= 20。

我不理解它是如何工作的弹性模量?

解决方案

从浮点其余的C#语言规范。在 X%Y 如果 X 的情况下,是正有限值。

  

以Z X%Y 的结果,并计算为 X - N * Y ,其中 N   是国内最大的可能整数小于或等于 X / Y

C#语言规范还明确列出了该怎么做了非零有限值,零,无穷的所有可能组合的情况下,表和NaN的可能出现浮动X%Y的点值。

  y值

                | + Y -y +0 -0 +∞-∞NaN的
           ----- + ----------------------------
  X + X | + Z + Z楠楠X X为NaN
            -x | -z -z楠楠-x -x为NaN
  v + 0 | + 0 + 0楠楠+ 0 + 0的NaN
  1-0 | -0 -0楠楠-0 -0的NaN
  L +∞|楠楠楠楠楠楠的NaN
  ü-∞|楠楠楠楠楠楠的NaN
  Ë为NaN |楠楠楠楠楠楠的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 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.

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

这篇关于如何模操作的工作方式与浮点数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆