~Simple number problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Stacey [MVP]

    ~Simple number problem

    if int is between 0-29 return 0. If 30-59, return 30.
    Can you do this with a one line math expression and no IF statements or bit
    shifting? TIA

    --
    William Stacey [MVP]



  • Bruce Wood

    #2
    Re: ~Simple number problem

    int i = (value / 30) * 30;

    Comment

    • Gabriel Magaña

      #3
      Re: ~Simple number problem

      Math.Floor(30 / x)*30;

      Which is much more expensive CPU-wise than

      y = (x > 30 ? 30 : 0);

      Is there a reason for these constraints (no if-then etc...) or is this just
      intellectial exercise?

      "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
      news:uSMFXS6IGH A.3728@tk2msftn gp13.phx.gbl...[color=blue]
      > if int is between 0-29 return 0. If 30-59, return 30.
      > Can you do this with a one line math expression and no IF statements or
      > bit
      > shifting? TIA
      >
      > --
      > William Stacey [MVP]
      >
      >
      >[/color]


      Comment

      • Gabriel Magaña

        #4
        Re: ~Simple number problem

        > Math.Floor(30 / x)*30;[color=blue]
        > Which is much more expensive CPU-wise than
        > y = (x > 30 ? 30 : 0);[/color]

        Actually I should shut up about this... The compiler team might have a
        super-fast implementation of Floor() in the library (ie, cast to int) and
        I'm just stating unsubstantiated ideas here...


        Comment

        • Bill Butler

          #5
          Re: ~Simple number problem


          "Gabriel Magaña" <no-spam@no-spam.com> wrote in message
          news:OGY8WY6IGH A.2900@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Math.Floor(30 / x)*30;
          >
          > Which is much more expensive CPU-wise than
          >
          > y = (x > 30 ? 30 : 0);
          >[/color]

          Although he didn't explicitly rule out (?:) , it is sort of cheating....don 't you think?

          Bill


          Comment

          • William Stacey [MVP]

            #6
            Re: ~Simple number problem

            Thanks Bruce!

            --
            William Stacey [MVP]

            "Bruce Wood" <brucewood@cana da.com> wrote in message
            news:1138409653 .860415.317870@ g43g2000cwa.goo glegroups.com.. .
            | int i = (value / 30) * 30;
            |


            Comment

            • William Stacey [MVP]

              #7
              Re: ~Simple number problem

              Thanks. It was for some sql thing I was doing so I needed an expression.

              --
              William Stacey [MVP]

              "Gabriel Magaña" <no-spam@no-spam.com> wrote in message
              news:OGY8WY6IGH A.2900@TK2MSFTN GP14.phx.gbl...
              | Math.Floor(30 / x)*30;
              |
              | Which is much more expensive CPU-wise than
              |
              | y = (x > 30 ? 30 : 0);
              |
              | Is there a reason for these constraints (no if-then etc...) or is this
              just
              | intellectial exercise?
              |
              | "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
              | news:uSMFXS6IGH A.3728@tk2msftn gp13.phx.gbl...
              | > if int is between 0-29 return 0. If 30-59, return 30.
              | > Can you do this with a one line math expression and no IF statements or
              | > bit
              | > shifting? TIA
              | >
              | > --
              | > William Stacey [MVP]
              | >
              | >
              | >
              |
              |


              Comment

              • Gabriel Magaña

                #8
                Re: ~Simple number problem

                > Although he didn't explicitly rule out (?:) , it is sort of[color=blue]
                > cheating....don 't you think?[/color]

                Yeh, it's really an if-then-else. I included it just for comparison's
                sake...


                Comment

                • C.C. \(aka Me\)

                  #9
                  Re: ~Simple number problem

                  Something like this?

                  int GetValue(int x)
                  {
                  return (x/30) * 30;
                  }

                  So:
                  x=0
                  (0/30)* 30 = 0

                  x=29
                  (29/30)*30 = 0

                  x=30
                  (30/30)*30 = 30

                  x=59
                  (59/30)*30 = 30 (need to round the 59/30 down or drop the decimal)


                  --
                  ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
                  Charles Cox
                  VC/VB/C# Developer
                  ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

                  "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
                  news:uSMFXS6IGH A.3728@tk2msftn gp13.phx.gbl...[color=blue]
                  > if int is between 0-29 return 0. If 30-59, return 30.
                  > Can you do this with a one line math expression and no IF statements or
                  > bit
                  > shifting? TIA
                  >
                  > --
                  > William Stacey [MVP]
                  >
                  >
                  >[/color]


                  Comment

                  • Bruce Wood

                    #10
                    Re: ~Simple number problem

                    You must be a young'un, William! That's an old, old programmer's trick
                    from back in the days before we had fancy stuff like Decimal.Round.

                    You kids don't know how good you have it these days. Back when I
                    started programming, we used to store six-digit dates to save a byte on
                    each one... (never dreaming of how much money that would make for all
                    of us twenty years on...)

                    Comment

                    • James Curran

                      #11
                      Re: ~Simple number problem

                      However, your question leave open what number to return in the int is
                      outside the range [0-59]. And the suggestion offered here give differenet
                      results for those values.

                      --
                      Truth,
                      James Curran
                      [erstwhile VC++ MVP]

                      Home: www.noveltheory.com Work: www.njtheater.com
                      Blog: www.honestillusion.com Day Job: www.partsearch.com
                      "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
                      news:uSMFXS6IGH A.3728@tk2msftn gp13.phx.gbl...[color=blue]
                      > if int is between 0-29 return 0. If 30-59, return 30.
                      > Can you do this with a one line math expression and no IF statements or[/color]
                      bit[color=blue]
                      > shifting? TIA
                      >
                      > --
                      > William Stacey [MVP]
                      >
                      >
                      >[/color]


                      Comment

                      • James Curran

                        #12
                        Re: ~Simple number problem

                        "Gabriel Magaña" <no-spam@no-spam.com> wrote in message
                        news:OGY8WY6IGH A.2900@TK2MSFTN GP14.phx.gbl...[color=blue]
                        > Math.Floor(30 / x)*30;
                        >
                        > Which is much more expensive CPU-wise than[/color]

                        And pointless more expensive, as that would be calculated as:

                        (int) (Math.Floor( (double) ((int) ((int) 30 / (int) x)) ) * (double)
                        30)

                        In other words, after calculating precisionly the value we want (an integer
                        0 or 1), we then promote it to a double, merely to call Math.Floor on it,
                        even though the integer division has already accomplished what we need. We
                        then do a floating-point multiple, before casting the number back down to an
                        int.

                        --
                        Truth,
                        James Curran
                        [erstwhile VC++ MVP]

                        Home: www.noveltheory.com Work: www.njtheater.com
                        Blog: www.honestillusion.com Day Job: www.partsearch.com


                        Comment

                        • William Stacey [MVP]

                          #13
                          Re: ~Simple number problem

                          :-) I wish! Old enouph to have been around when ATT System V was king.
                          After I saw it, I smacked my head - doh. Cheers Bruce.

                          --
                          William Stacey [MVP]

                          "Bruce Wood" <brucewood@cana da.com> wrote in message
                          news:1138605697 .783489.173160@ g14g2000cwa.goo glegroups.com.. .
                          | You must be a young'un, William! That's an old, old programmer's trick
                          | from back in the days before we had fancy stuff like Decimal.Round.
                          |
                          | You kids don't know how good you have it these days. Back when I
                          | started programming, we used to store six-digit dates to save a byte on
                          | each one... (never dreaming of how much money that would make for all
                          | of us twenty years on...)
                          |


                          Comment

                          Working...