Christoph
Christoph

Reputation: 448

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. 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)

This formula is applied to 4 objects. Theta is in all objects declared as float with the value 1,5708. Alpha is initialized with 0 and will be increased by each iteration.

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! 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.

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. But also when i place (1/2) at the end it results in 0.0. Does anyone here have an idea why?

Upvotes: 1

Views: 143

Answers (6)

Joni
Joni

Reputation: 111239

If you write 1/2 the result is calculated using integer division that gives an integer result. You can force a floating point division by changing one of the numbers to a floating point number, as in 1/2f.

Or you could just write 0.5 which IMHO is more readable than 1/2.

Upvotes: 3

jason
jason

Reputation: 241641

This

(1 / 2)

evaluates to 0 because it's integer division. 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.

Upvotes: 8

phoog
phoog

Reputation: 43046

Why multiply by 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; 

Upvotes: 1

Guffa
Guffa

Reputation: 700302

That's because 1 and 2 are integer values, not floating point values.

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.

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. Then the result is divided by 2, which will also be implicitly converted into a floating point value.

So what you end up doing is:

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

Upvotes: 0

Scott Pedersen
Scott Pedersen

Reputation: 1311

Because 1/2 is treated as integer arithmetic and as such is rounded to 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.

Upvotes: 0

BlueM
BlueM

Reputation: 6888

You should write 1.0/2 or 0.5 instead 1/2.

1/2 is an integer division which results in an integer 0.

Upvotes: 0

Related Questions