Math.IEEERemainder返回负的成绩。为什么?成绩、Math、IEEERemainder

2023-09-03 03:39:26 作者:━═☆莪の啈冨☆╰︶ ̄

.NET Framework包括Math.IEEERemainder(X,Y),除了标准的Mod运算符。这是什么功能,真的在做什么?我不明白的负数,这产生。

例如:

  Math.IEEERemainder(0,2)= 0
Math.IEEERemainder(1,2)= 1
Math.IEEERemainder(2,2)= 0
Math.IEEERemainder(3,2)= -1
 

解决方案

如果你读的 System.Math.IEEERemainder的MSDN页面,你会发现,两个正数可以有一个负面的剩余部分。

  

返回值

     Excel学会这一招,轻松搞定所有查找难题

一个数等于 X - (Y Q)的,其中的问:的是商的 X / Y 的四舍五入为最接近的整数(如果的 X / Y 的属于中间两个整数之间,则返回偶数整数)。

所以:3 - (2 *(轮(3/2)))= -1

  / *
...
分隔两个双precision浮点值:
1.797693e + 308 / 2.00:1)的IEEE余数为0.000000e + 000
2)在IEEE 1.797693e + 308 / 3.00的其余部分是-1.000000e + 800
需要注意的是两个正数可以产生负的余数。

* /
 

后记

实际的问题可能是,为什么我们有两个余数的操作?当处理  浮点数据,你总是需要认识到你的浮点标准。由于我们是在21世纪,几乎所有的内容是IEEE 754和极少数的我们担心的说VAX F_Float与IEEE 754。

借助 C#标准指出,余运算符(节7.7.3)中,当施加到浮点参数,当应用于整数参数是类似的余数运算符。也就是说,同样的数学公式 1 使用(与其他注意事项与浮点再presentations相关的极端情况)的整数和浮点运算剩余

因此​​,如果你正在寻找让你的其余部分的运算浮点数符合你当前的IEEE 754舍入模式,最好是使用 Math.IEEERemainder 。但是,如果您使用不是特别敏感的C#余运算符四舍五入产生的细微差别,然后继续使用操作。

在给定:Z = X%Y,则Z = X - (X / Y)* Y

The .net framework includes Math.IEEERemainder(x, y) in addition to the standard mod operator. What is this function really doing? I dont understand the negative numbers that this produces.

Example:

Math.IEEERemainder(0, 2) = 0
Math.IEEERemainder(1, 2) = 1
Math.IEEERemainder(2, 2) = 0
Math.IEEERemainder(3, 2) = -1

解决方案

If you read the example given at System.Math.IEEERemainder's MSDN page, you'll notice that two positive numbers can have a negative remainder.

Return Value

A number equal to x - (y Q), where Q is the quotient of x / y rounded to the nearest integer (if x / y falls halfway between two integers, the even integer is returned).

So: 3 - (2 * (round(3 / 2))) = -1

/*
...
Divide two double-precision floating-point values:
1) The IEEE remainder of 1.797693e+308/2.00 is 0.000000e+000
2) The IEEE remainder of 1.797693e+308/3.00 is -1.000000e+000
Note that two positive numbers can yield a negative remainder.

*/

Epilogue

The actual question could be, "Why do we have two remainder operations?" When dealing with floating point data, you always need to be cognizant of your floating point standard. Since we're in the 21st century, most everything is on IEEE 754 and very few of us worry about say VAX F_Float versus IEEE 754.

The C# standard states that the remainder operator (Section 7.7.3), when applied to floating point arguments, is analogous to the remainder operator when applied to integer arguments. That is, the same mathematical formula1 is used (with additional considerations for corner cases associated with floating point representations) in both integer and floating point remainder operations.

Therefore, if you are looking to have your remainder operations on floating point numbers conform to your current IEEE 754 rounding modes, it is advisable to use Math.IEEERemainder. However, if your usage is not particularly sensitive to the subtle difference in rounding produced by the C# remainder operator, then continue using the operator.

Given: z = x % y, then z = x - (x / y) * y