模数运算如何与浮点数据类型一起使用?

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 的情况下,如果 xy 是正有限值.

<块引用>

zx % 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 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

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

本文标题为:模数运算如何与浮点数据类型一起使用?

基础教程推荐