Division Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TisMe

    #1

    Division Question

    Hi All,

    The code below works as expected, returning 0.2857143

    public partial class tests : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    Single myVal = (float)200.00 / (float)700.00;
    this.Label1.Tex t = 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!

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Division Question

    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.

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

    "TisMe" <simonmharris@g ooglemail.comwr ote in message
    news:0b714902-10a8-4bab-8dbd-c2155823ea5b@f3 g2000hsg.google groups.com...
    Hi All,
    >
    The code below works as expected, returning 0.2857143
    >
    public partial class tests : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    Single myVal = (float)200.00 / (float)700.00;
    this.Label1.Tex t = 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!
    >

    Comment

    • RobertK

      #3
      Re: Division Question

      Single myVal = ( float )200 / ( float )700;
      this.label1.Tex t = myVal.ToString( );
      produces the right answer, 0.2857143, when I run it.

      Comment

      • Peter Duniho

        #4
        Re: Division Question

        On Mon, 10 Dec 2007 09:52:19 -0800, TisMe <simonmharris@g ooglemail.com>
        wrote:
        [...]
        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

        Comment

        • Charles Calvert

          #5
          Re: Division Question

          [top-posting reversed]

          On Mon, 10 Dec 2007 13:39:42 -0500, "Nicholas Paldino [.NET/C# MVP]"
          <mvp@spam.guard .caspershouse.c omwrote in
          <eHptot1OIHA.35 56@TK2MSFTNGP03 .phx.gbl>:
          >"TisMe" <simonmharris@g ooglemail.comwr ote in message
          >news:0b71490 2-10a8-4bab-8dbd-c2155823ea5b@f3 g2000hsg.google groups.com...
          >>
          >The code below works as expected, returning 0.2857143
          >>
          >public partial class tests : System.Web.UI.P age
          >{
          > protected void Page_Load(objec t sender, EventArgs e)
          > {
          > Single myVal = (float)200.00 / (float)700.00;
          > this.Label1.Tex t = 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?
          >
          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
          > 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?
          --
          Charles Calvert | Software Design/Development
          Celtic Wolf, Inc. | Project Management
          http://www.celticwolf.com/ | Technical Writing
          (703) 580-0210 | Research

          Comment

          • Bill Butler

            #6
            Re: Division Question

            "Charles Calvert" <cbciv@yahoo.co mwrote in message
            news:dghrl3tfpn 4f370dcdgu481b4 4jkbkrf4d@4ax.c om...
            [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
            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?

            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


            Comment

            • Charles Calvert

              #7
              Re: Division Question

              On Mon, 10 Dec 2007 23:33:07 GMT, "Bill Butler" <qwerty@asdf.co m>
              wrote in <T0k7j.5815$581 .2895@trnddc04> :
              >"Charles Calvert" <cbciv@yahoo.co mwrote in message
              >news:dghrl3tfp n4f370dcdgu481b 44jkbkrf4d@4ax. com...
              >[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
              >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?
              >
              >
              >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.
              --
              Charles Calvert | Software Design/Development
              Celtic Wolf, Inc. | Project Management
              http://www.celticwolf.com/ | Technical Writing
              (703) 580-0210 | Research

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Division Question

                Charles Calvert <cbciv@yahoo.co mwrote:

                <snip>
                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?
                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.WriteLi ne (myVal);
                }
                }

                prints 0.2857143.

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

                Comment

                • Bill Butler

                  #9
                  Re: Division Question


                  "Charles Calvert" <cbciv@yahoo.co mwrote in message
                  news:avjrl3lhe3 jn0gmedf58jmf4i jnu2lk8pn@4ax.c om...
                  On Mon, 10 Dec 2007 23:33:07 GMT, "Bill Butler" <qwerty@asdf.co m>
                  >
                  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


                  Comment

                  Working...