[c#] Weird Stuff

Joined
Jan 26, 2006
Messages
483
Reaction score
6
Hey,

So I currently finished my little gamble application.
Its all working fine except for the percentage function.
We know you can calculate the percentage by doing:
Code:
times you won / amount of games * 100
Now I put this in c# with the variables:
Code:
double winper, loseper;
int good = 0, amountgames = 0, wrong = 0;

winper = good / amountgames * 100;
textBox5.Text = winper.ToString();

loseper = wrong / amountgames * 100;
textBox6.Text = loseper.ToString();

Now when you gamble the right number it will do good++ and show 100% win which is good.
But when you lose it will do wrong++

When this happens the trouble starts it will give both winper and loseper the value of zero.

I cant find out even with the debugger why hes giving it zero there is no logical reason for this...

Thanks

-Darkco
 
It makes perfect sense to me, you forgot a couple pairs of parenthesis.

Your math is saying, 0 divided by 0 = error, times 100 equals 0.

What did you expect to happen?

You cannot divide by 0, period.

Now, if the numbers were right, then this is probably what you want:
Code:
winper = good / (amountgames*100);
 
Last edited:
Ahh yup that was just to show which kind of variables im using.

The whole program is about 120 lines which will increment ofc.
Anyway I tried with the parenthesis but no luck. In the debugger it will still assign the value zero even when I remove the * 100.

So for example when I won 1 game and lost 2 games it will show in both textboxs 0.
But when I calculate 1 / 3 = 0,333333333333 like I said its weird.
 
Ahh yup that was just to show which kind of variables im using.

The whole program is about 120 lines which will increment ofc.
Anyway I tried with the parenthesis but no luck. In the debugger it will still assign the value zero even when I remove the * 100.

So for example when I won 1 game and lost 2 games it will show in both textboxs 0.
But when I calculate 1 / 3 = 0,333333333333 like I said its weird.

You can use Math.Floor, Math.Ceil, or other rounding functions.
 
100 times 0 is just as much zero as simple 0, so parenthesis don't make a difference. Besides, I think a divide by zero should give something other than a 0, but then again I'm no C# expert.

I'm not sure which way those conversions go, if any, but see if it makes a temporary conversion to int (obviously losing decimals there) where you divide an int by something else (double).
 
Last edited:
thanks Negata you helped me once more with my question ^^

Anyway for the people who maybe misunderstand me I never divided by zero when I do VS will give a DivideByZeroException which is a pretty clear error to me ^^

And my problem the variables good , amountgames, wrong those should be double cause with int they aint working still trying to figure out why but atleast its working.
Also when I try to divide with zero with a int it will crash but with a double it will just say Infinity.
 
Last edited:
thanks Negata you helped me once more with my question ^^
I win. :)
And my problem the variables good , amountgames, wrong those should be double cause with int they aint working still trying to figure out why but atleast its working.
I may be a little off here, but it's basically this: in its inner workings the calculation obviously is performed in parts, one part of it being say 1/3. As the calculation proceeds from right to left there, the data type of the temporary value (just before it's finally assigned to your designated float) is assumed from, in this case, the leftmost operand, which was int, so the whole result is first converted to that.

Now, knowing the reason, it should be fairly easy to see that a slight adjustment to the order of your operations should also provide a proper result without the need of changing your data types. Also, there's always the option of casting your int to double.
 
Last edited:
Ahh yup I understand :)

So I tried giving amountgames (which is before *100) a double and this works great. I didnt know that it was calculating in parts anyway I must give amountgames a double ofc cause you get a decimal digit. Just weird that the debugger wont show this error and just make stupidly zero of it.

But problem solved and learned something new ^^
Thanks again

PS: are you sure the calculation proceeds from right to left at 1/3 cause that would give you 3 and not 0,333333 I think you mean left to right...
 
Last edited:
So I tried giving amountgames (which is before *100) a double and this works great. I didnt know that it was calculating in parts anyway I must give amountgames a double ofc cause you get a decimal digit. Just weird that the debugger wont show this error and just make stupidly zero of it.
That's because it's not really an error. Rather often it's the desired result, actually: easy rounding down. The simple solution I mentioned above as an alternative to going double is doing the multiplication first.
PS: are you sure the calculation proceeds from right to left at 1/3 cause that would give you 3 and not 0,333333 I think you mean left to right...
Good point, no, I wasn't thinking what I was saying (that was so wrong in terms of both common sense and direction of associativity). What I meant was for the first calculation the operand on the left was the target, and from which the resulting datatype was taken. When I said right before assignment I had already simplified out the multiplication part in my mind as it wasn't relevant to the real problem. :) My bad, should've been more accurate there.
 
Back