Division Question

  • Thread starter Thread starter TisMe
  • Start date Start date
T

TisMe

Hi All,

The code below works as expected, returning 0.2857143

public partial class tests : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Single myVal = (float)200.00 / (float)700.00;
this.Label1.Text = myVal.ToString();
}

}

How ever, if I change the 200.00 to 200 and the 700.00 to 700, the
result is 0.

Can anyone tell me why this happens? Surely dividing a float by a
float should return a float? I can easily change the values in my
simple example, but how would I fix this problem in my application?
e.g. How do I change a variables value from 200 to 200.00?

Thank you!
 
Single myVal = ( float )200 / ( float )700;
this.label1.Text = myVal.ToString( );
produces the right answer, 0.2857143, when I run it.
 
TisMe,

The reason for this is because the C# compiler sees an integer literal,
and does the equivalent of this (without declaring the variables):

int temp1 = 200;
int temp2 = 700;
Single myVal = temp1 / temp2;

And with integer division, 200 divided by 700 is 0. That then gets
converted to a Single.
 
[...]
How ever, if I change the 200.00 to 200 and the 700.00 to 700, the
result is 0.

You should post an example of code that _doesn't_ work. Posting the code
that works isn't very helpful, because we have no way to know exactly what
it is you're doing.

In particular, if we do what you say and simply replace "200.00" with
"200" and "700.00" with "700" in the code you posted, everything works
fine.
Can anyone tell me why this happens? Surely dividing a float by a
float should return a float?

It should, and it does. If you're getting 0, then neither of the operands
are actually floats. Which suggests you did more than just replace the
numbers; you must have left out the type-cast to float as well.

The fix is to make sure at least one of the operands is typed as float.
You can either cast it, or you can add the letter "f" at the end of the
constant (e.g. "200f") to indicate it's a float.
I can easily change the values in my
simple example, but how would I fix this problem in my application?
e.g. How do I change a variables value from 200 to 200.00?

This seems to be a different question. You don't have any variables in
the code example you posted that have a value of 200. So it's not
possible to answer how to change it to "200.00" (noting, of course, that
the two values are actually equal...I presume you really just mean how to
convert the type, not the value).

Pete
 
[top-posting reversed]

The reason for this is because the C# compiler sees an integer literal,
and does the equivalent of this (without declaring the variables):

int temp1 = 200;
int temp2 = 700;
Single myVal = temp1 / temp2;

I checked the operator precedence table for 1.1
(<http://msdn2.microsoft.com/en-us/library/aa691323(VS.71).aspx>) and
cast (a unary operation) takes precedence over "/" (a multiplicative
operation), so shouldn't

be equivalent to:

int temp1 = 200;
int temp2 = 700;
float temp3 = temp1;
float temp4 = temp2;
Single myVal = temp3 / temp4;

Am I missing something?
 
Charles Calvert said:
[top-posting reversed]
I checked the operator precedence table for 1.1
(<http://msdn2.microsoft.com/en-us/library/aa691323(VS.71).aspx>) and
cast (a unary operation) takes precedence over "/" (a multiplicative
operation), so shouldn't


be equivalent to:

int temp1 = 200;
int temp2 = 700;
float temp3 = temp1;
float temp4 = temp2;
Single myVal = temp3 / temp4;

Am I missing something?


As others have already posted

(float)200 / (float)700 gives the correct answer....not 0.

Perhaps you can post a short but complete program that demonstrates your
problem

Bill
 
Charles Calvert said:
[top-posting reversed]
I checked the operator precedence table for 1.1
(<http://msdn2.microsoft.com/en-us/library/aa691323(VS.71).aspx>) and
cast (a unary operation) takes precedence over "/" (a multiplicative
operation), so shouldn't


be equivalent to:

int temp1 = 200;
int temp2 = 700;
float temp3 = temp1;
float temp4 = temp2;
Single myVal = temp3 / temp4;

Am I missing something?


As others have already posted

(float)200 / (float)700 gives the correct answer....not 0.

Perhaps you can post a short but complete program that demonstrates your
problem

Thanks, but it's not my problem. I was just participating in the
thread. My response was to Nicholas Paldino, whose answer didn't make
sense to me.
 
be equivalent to:

int temp1 = 200;
int temp2 = 700;
float temp3 = temp1;
float temp4 = temp2;
Single myVal = temp3 / temp4;

Am I missing something?

I suspect that Nicholas thought (as I did) that your code which didn't
work was:

Single myVal = 200/700;

If it was in fact:

Single myVal = (float)200 / (float)700;

then I believe you're misinterpreting your results somehow, as that
certainly *won't* leave myVal as 0. For instance:

using System;

class Test
{
static void Main()
{
Single myVal = (float)200 / (float)700;
Console.WriteLine (myVal);
}
}

prints 0.2857143.
 
Charles Calvert said:
On Mon, 10 Dec 2007 23:33:07 GMT, "Bill Butler" <[email protected]>

Thanks, but it's not my problem. I was just participating in the
thread. My response was to Nicholas Paldino, whose answer didn't make
sense to me.


Oops...sorry about that...not sure what I was thinking <grin>

Bill
 
I suspect that Nicholas thought (as I did) that your code which didn't
work was:

Except that it's not my code. I was just participating in the thread.
;)
Single myVal = 200/700;

If it was in fact:

Single myVal = (float)200 / (float)700;

then I believe you're misinterpreting your results somehow, as that
certainly *won't* leave myVal as 0. For instance:

using System;

class Test
{
static void Main()
{
Single myVal = (float)200 / (float)700;
Console.WriteLine (myVal);
}
}

prints 0.2857143.

Right. I don't know where the OP got 0, but I suspect that the code
he posted is not exactly the code he tested.
 
Back
Top