Really Simple Math

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

    #1

    Really Simple Math

    I've declared o to be a double and c an int. No matter what the value of c,
    o is never greater than 0 after the following line of code:

    o = (c / 100);


    Any ideas why?


  • Dennis Roark

    #2
    Re: Really Simple Math

    "Christophe r Weaver" <we.aver@verizo n.net> wrote in
    news:uTS#76LYFH A.584@TK2MSFTNG P15.phx.gbl:
    [color=blue]
    > I've declared o to be a double and c an int. No matter what the value
    > of c, o is never greater than 0 after the following line of code:
    >
    > o = (c / 100);
    >
    >
    > Any ideas why?
    >
    >
    >[/color]
    c/100 performs integer division. If c is less than 100 then c/100 will
    be a 0. If you want the value you expect, use

    o = c / 100.0 now you do floating point division.
    Any introductory programming book will go over this.

    (BTW, o is a poor name of a variable of 1 character length.)


    --
    Dennis Roark

    denro@sio.NOSPA Mmidco.net
    Starting Points:

    Comment

    • Christopher Weaver

      #3
      Re: Really Simple Math

      Thanks. I knew it was basic. In fact, as I read your reply I recalled the
      point being made in my first semester c++ class. Years of Pascal has erased
      much of that.

      BTW, the 'o' was included for debugging purposes only.


      "Dennis Roark" <denro@NOSPsio. midco.net> wrote in message
      news:Xns9660C60 5932A7denromidc onet@216.196.97 .142...[color=blue]
      > "Christophe r Weaver" <we.aver@verizo n.net> wrote in
      > news:uTS#76LYFH A.584@TK2MSFTNG P15.phx.gbl:
      >[color=green]
      >> I've declared o to be a double and c an int. No matter what the value
      >> of c, o is never greater than 0 after the following line of code:
      >>
      >> o = (c / 100);
      >>
      >>
      >> Any ideas why?
      >>
      >>
      >>[/color]
      > c/100 performs integer division. If c is less than 100 then c/100 will
      > be a 0. If you want the value you expect, use
      >
      > o = c / 100.0 now you do floating point division.
      > Any introductory programming book will go over this.
      >
      > (BTW, o is a poor name of a variable of 1 character length.)
      >
      >
      > --
      > Dennis Roark
      >
      > denro@sio.NOSPA Mmidco.net
      > Starting Points:
      > http://sio.midco.net/denro/www[/color]


      Comment

      • Rick Lones

        #4
        Re: Really Simple Math

        Christopher Weaver wrote:[color=blue]
        > I've declared o to be a double and c an int. No matter what the value of c,
        > o is never greater than 0 after the following line of code:
        >
        > o = (c / 100);
        >
        >
        > Any ideas why?
        >
        >[/color]
        No idea IF your claim is correct. However, I think what you really mean is "No
        matter what the value of c (as long as it's less than 100), o is never greater
        than 0."

        The expression (c / 100) is apparently calculated using integer arithmetic,
        which is perfectly reasonable since c and 100 are both integer expressions. The
        integer result of this division will be zero when c is less than 100. I suggest
        you test your "no matter what" claim with some larger values of c (e.g., 100,
        101, 199, 200, 201, 299, etc.) and watch what happens.

        -rick-

        Comment

        • Altramagnus

          #5
          Re: Really Simple Math

          Personally I prefer to use:

          o = c * 0.01;

          Is it faster?
          Perharps negligible.


          "Christophe r Weaver" <we.aver@verizo n.net> wrote in message
          news:ej27AHMYFH A.1092@tk2msftn gp13.phx.gbl...[color=blue]
          > Thanks. I knew it was basic. In fact, as I read your reply I recalled
          > the point being made in my first semester c++ class. Years of Pascal has
          > erased much of that.
          >
          > BTW, the 'o' was included for debugging purposes only.
          >
          >
          > "Dennis Roark" <denro@NOSPsio. midco.net> wrote in message
          > news:Xns9660C60 5932A7denromidc onet@216.196.97 .142...[color=green]
          >> "Christophe r Weaver" <we.aver@verizo n.net> wrote in
          >> news:uTS#76LYFH A.584@TK2MSFTNG P15.phx.gbl:
          >>[color=darkred]
          >>> I've declared o to be a double and c an int. No matter what the value
          >>> of c, o is never greater than 0 after the following line of code:
          >>>
          >>> o = (c / 100);
          >>>
          >>>
          >>> Any ideas why?
          >>>
          >>>
          >>>[/color]
          >> c/100 performs integer division. If c is less than 100 then c/100 will
          >> be a 0. If you want the value you expect, use
          >>
          >> o = c / 100.0 now you do floating point division.
          >> Any introductory programming book will go over this.
          >>
          >> (BTW, o is a poor name of a variable of 1 character length.)
          >>
          >>
          >> --
          >> Dennis Roark
          >>
          >> denro@sio.NOSPA Mmidco.net
          >> Starting Points:
          >> http://sio.midco.net/denro/www[/color]
          >
          >[/color]


          Comment

          • Christopher Weaver

            #6
            Re: Really Simple Math

            Good point. It's so nice to have a group that pays attention and cares
            enough to write.


            "Rick Lones" <WrlonesX@Ychar terZ.net> wrote in message
            news:TOPke.1038 2$eR.8486@fe05. lga...[color=blue]
            > Christopher Weaver wrote:[color=green]
            >> I've declared o to be a double and c an int. No matter what the value of
            >> c, o is never greater than 0 after the following line of code:
            >>
            >> o = (c / 100);
            >>
            >>
            >> Any ideas why?[/color]
            > No idea IF your claim is correct. However, I think what you really mean
            > is "No matter what the value of c (as long as it's less than 100), o is
            > never greater than 0."
            >
            > The expression (c / 100) is apparently calculated using integer
            > arithmetic, which is perfectly reasonable since c and 100 are both integer
            > expressions. The integer result of this division will be zero when c is
            > less than 100. I suggest you test your "no matter what" claim with some
            > larger values of c (e.g., 100, 101, 199, 200, 201, 299, etc.) and watch
            > what happens.
            >
            > -rick-[/color]


            Comment

            • Christopher Weaver

              #7
              Re: Really Simple Math

              I think I like it better too.


              "Altramagnu s" <altramagnus@ho tmail.com> wrote in message
              news:4293d9bf$1 @news.starhub.n et.sg...[color=blue]
              > Personally I prefer to use:
              >
              > o = c * 0.01;
              >
              > Is it faster?
              > Perharps negligible.
              >
              >
              > "Christophe r Weaver" <we.aver@verizo n.net> wrote in message
              > news:ej27AHMYFH A.1092@tk2msftn gp13.phx.gbl...[color=green]
              >> Thanks. I knew it was basic. In fact, as I read your reply I recalled
              >> the point being made in my first semester c++ class. Years of Pascal has
              >> erased much of that.
              >>
              >> BTW, the 'o' was included for debugging purposes only.
              >>
              >>
              >> "Dennis Roark" <denro@NOSPsio. midco.net> wrote in message
              >> news:Xns9660C60 5932A7denromidc onet@216.196.97 .142...[color=darkred]
              >>> "Christophe r Weaver" <we.aver@verizo n.net> wrote in
              >>> news:uTS#76LYFH A.584@TK2MSFTNG P15.phx.gbl:
              >>>
              >>>> I've declared o to be a double and c an int. No matter what the value
              >>>> of c, o is never greater than 0 after the following line of code:
              >>>>
              >>>> o = (c / 100);
              >>>>
              >>>>
              >>>> Any ideas why?
              >>>>
              >>>>
              >>>>
              >>> c/100 performs integer division. If c is less than 100 then c/100 will
              >>> be a 0. If you want the value you expect, use
              >>>
              >>> o = c / 100.0 now you do floating point division.
              >>> Any introductory programming book will go over this.
              >>>
              >>> (BTW, o is a poor name of a variable of 1 character length.)
              >>>
              >>>
              >>> --
              >>> Dennis Roark
              >>>
              >>> denro@sio.NOSPA Mmidco.net
              >>> Starting Points:
              >>> http://sio.midco.net/denro/www[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Really Simple Math

                Altramagnus <altramagnus@ho tmail.com> wrote:[color=blue]
                > Personally I prefer to use:
                >
                > o = c * 0.01;
                >
                > Is it faster?
                > Perharps negligible.[/color]

                I don't think it's nearly as clear at a glance that that's dividing by
                a hundred though. Just MHO.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Altramagnus

                  #9
                  Re: Really Simple Math

                  i agree, but you can put a comment or something.


                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.1cfe29 b83816ee8098c1b e@msnews.micros oft.com...[color=blue]
                  > Altramagnus <altramagnus@ho tmail.com> wrote:[color=green]
                  >> Personally I prefer to use:
                  >>
                  >> o = c * 0.01;
                  >>
                  >> Is it faster?
                  >> Perharps negligible.[/color]
                  >
                  > I don't think it's nearly as clear at a glance that that's dividing by
                  > a hundred though. Just MHO.
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Really Simple Math

                    Altramagnus <altramagnus@ho tmail.com> wrote:[color=blue]
                    > i agree, but you can put a comment or something.[/color]

                    If you have to put a comment on one version to make it clearer, and you
                    haven't proved it to be faster, why do you prefer it? I'd take
                    readability over unproven speed any day.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    Working...