简体   繁体   English

为什么这会导致0,而倒置却不会呢?

[英]Why does this result in 0 and upsidedown not?

Currently I am writing my thesis and I was confronted with a behavior of .Net C# that I had never seen before. 目前,我正在写论文,而且遇到了从未见过的.Net C#行为。 I am talking about an error in a calculation. 我说的是计算中的错误。 I implemented this formula: 我实现了这个公式:

1/2 * (Theta i-1 + Theta i) + Sum(Alph k, k=1, i-1) 1/2 *(Theta i-1 + Theta i)+ Sum(Alph k,k = 1,i-1)

This formula is applied to 4 objects. 此公式适用于4个对象。 Theta is in all objects declared as float with the value 1,5708. Theta在所有声明为float的对象中,其值为1,5708。 Alpha is initialized with 0 and will be increased by each iteration. Alpha初始化为0,并且每次迭代都会增加。

First implmentation 第一次实施

    float alpha = 0;
    float value = 0;
    for (int sphereCount = 1; sphereCount < this.spheres.Count; sphereCount++)
    {
        value = (1/2) * (this.spheres[sphereCount - 1].Theta + this.spheres[sphereCount].Theta);
        alpha += value;
    }

With this version value is always 0.0! 使用此版本值始终为0.0! So I changed it to: 所以我将其更改为:

Working implementaion 工作实施

    float alpha = 0;
    float value = 0;
    for (int sphereCount = 1; sphereCount < this.spheres.Count; sphereCount++)
    {
        value =(this.spheres[sphereCount - 1].Theta + this.spheres[sphereCount].Theta) * 1/2;
        alpha += value;
    }

By removing the brackets around the 1/2 and placing it at the end of the calculation it worked. 通过移除1/2周围的括号并将其放在计算的末尾,它可以正常工作。

WHY IS THAT SO??? 为什么会这样???

It seems when you place 1/2 in brackets not depending on the position of 1/2 the result is 0.0. 似乎将1/2放在方括号中而不取决于1/2的位置时,结果是0.0。 But also when i place (1/2) at the end it results in 0.0. 而且当我将(1/2)放在最后时,它也会导致0.0。 Does anyone here have an idea why? 这里有人知道为什么吗?

This 这个

(1 / 2)

evaluates to 0 because it's integer division. 计算为0,因为它是整数除法。 If you say 如果你说

(1 / 2f)

or 要么

(1 / (float) 2)

you'll be fine because it forces float divsion. 您会好起来的,因为它会强制浮动除法。 Or, even better, just write 0.5 . 或者,甚至更好,只写0.5

If you write 1/2 the result is calculated using integer division that gives an integer result. 如果您写1/2,则使用给出整数结果的整数除法来计算结果。 You can force a floating point division by changing one of the numbers to a floating point number, as in 1/2f . 您可以通过将数字之一更改为浮点数来强制进行浮点除法,如1/2f

Or you could just write 0.5 which IMHO is more readable than 1/2. 或者,您可以只写0.5 ,恕我直言,它比1/2更易读。

Why multiply by 1 ? 为什么要乘以1 Rather than this: 而不是这样:

value =(this.spheres[sphereCount - 1].Theta + this.spheres[sphereCount].Theta) * 1/2; 

why not write this: 为什么不这样写:

value =(this.spheres[sphereCount - 1].Theta + this.spheres[sphereCount].Theta) / 2; 

You should write 1.0/2 or 0.5 instead 1/2. 您应该写1.0 / 2或0.5而不是1/2。

1/2 is an integer division which results in an integer 0. 1/2是一个整数除法,得出整数0。

Because 1/2 is treated as integer arithmetic and as such is rounded to 0. 因为1/2被视为整数算术,因此四舍五入为0。

Removing the parenthesis changes the order of operations, and now you are dividing your whole (floating point) formula by two and arriving at a floating point answer. 删除括号会更改操作顺序,现在您将整个(浮点)公式除以二,得出一个浮点答案。

That's because 1 and 2 are integer values, not floating point values. 这是因为12是整数值,而不是浮点值。

When you divide the integer value 1 by the integer value 2 , the result is the integer value 0 , not the floating point value 0.5 . 当您将整数值1除以整数值2 ,结果是整数值0 ,而不是浮点值0.5

When you remove the parentheses and change the order of the multiplication, the other part of the expression will first be multiplied by 1 , which will implicitly convert the integer value 1 into a floating point value. 当您删除括号并更改乘法顺序时,表达式的另一部分将首先乘以1 ,这将隐式将整数值1转换为浮点值。 Then the result is divided by 2 , which will also be implicitly converted into a floating point value. 然后将结果除以2 ,这也将隐式转换为浮点值。

So what you end up doing is: 因此,您最终要做的是:

value = ( (this.spheres[sphereCount - 1].Theta + this.spheres[sphereCount].Theta) * (float)1 ) / (float)2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM