Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Division Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TisMe - 10 Dec 2007 17:52 GMT
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!
RobertK - 10 Dec 2007 18:35 GMT
Single myVal = ( float )200 / ( float )700;
 this.label1.Text = myVal.ToString( );
produces the right answer,  0.2857143, when I run it.
Nicholas Paldino [.NET/C# MVP] - 10 Dec 2007 18:39 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi All,
>
[quoted text clipped - 19 lines]
>
> Thank you!
Charles Calvert - 10 Dec 2007 23:13 GMT
[top-posting reversed]

>> The code below works as expected, returning 0.2857143
>>
[quoted text clipped - 22 lines]
>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

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

be equivalent to:

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

Am I missing something?
Signature

Charles Calvert             |  Software Design/Development
Celtic Wolf, Inc.           |  Project Management
http://www.celticwolf.com/  |  Technical Writing
(703) 580-0210              |  Research

Bill Butler - 10 Dec 2007 23:33 GMT
> [top-posting reversed]
<snip>
> I checked the operator precedence table for 1.1
> (<http://msdn2.microsoft.com/en-us/library/aa691323(VS.71).aspx>) and
[quoted text clipped - 12 lines]
>
> 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 - 10 Dec 2007 23:47 GMT
>> [top-posting reversed]
><snip>
[quoted text clipped - 21 lines]
>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.
Signature

Charles Calvert             |  Software Design/Development
Celtic Wolf, Inc.           |  Project Management
http://www.celticwolf.com/  |  Technical Writing
(703) 580-0210              |  Research

Bill Butler - 11 Dec 2007 00:28 GMT
> On Mon, 10 Dec 2007 23:33:07 GMT, "Bill Butler" <qwerty@asdf.com>
>
> 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
Jon Skeet [C# MVP] - 11 Dec 2007 00:21 GMT
<snip>

> >>        Single myVal = (float)200 / (float)700;
>
[quoted text clipped - 7 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Charles Calvert - 11 Feb 2008 19:59 GMT
><snip>
>
[quoted text clipped - 12 lines]
>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;
>
[quoted text clipped - 17 lines]
>
>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.
Signature

Charles Calvert             |  Software Design/Development
Celtic Wolf, Inc.           |  Project Management
http://www.celticwolf.com/  |  Technical Writing
(703) 580-0210              |  Research

Peter Duniho - 10 Dec 2007 18:49 GMT
> [...]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.