Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Rounding up to a multiple of a number

Rounding up to a multiple of a number

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Remco Hoogenboezem
    wrote on last edited by
    #1

    I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3. I wrote the following code to do this: lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; In this piece of code lMinimum is de value that gets rounded up. And lIncrement is the value to wich lMinimum is rounded up. So if we choose the numbers mentioned above we get 6. This works fine for positive numbers but when we choose a negative number for lMinimum it goes wrong. I wrote another piece of code to handle negative numbers lCurrent=abs(lMinimum%lIncrement)+lMinimum; So combining the two we get: if(lMinimum>=0) { lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; } else { lCurrent=abs(lMinimum%lIncrement)+lMinimum; } This is the obvious way and it contains one conditional branch. I was wondering of someone came accros a bit twiddling hack to do this more efficient. Thanks ;)

    N L 2 Replies Last reply
    0
    • R Remco Hoogenboezem

      I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3. I wrote the following code to do this: lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; In this piece of code lMinimum is de value that gets rounded up. And lIncrement is the value to wich lMinimum is rounded up. So if we choose the numbers mentioned above we get 6. This works fine for positive numbers but when we choose a negative number for lMinimum it goes wrong. I wrote another piece of code to handle negative numbers lCurrent=abs(lMinimum%lIncrement)+lMinimum; So combining the two we get: if(lMinimum>=0) { lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; } else { lCurrent=abs(lMinimum%lIncrement)+lMinimum; } This is the obvious way and it contains one conditional branch. I was wondering of someone came accros a bit twiddling hack to do this more efficient. Thanks ;)

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Remco Hoogenboezem wrote:

      I have got the number 5 and i would like to round it up to a multiple of 3.

      From the example you suggested...

      LONG round = 5 - ( 5 % 3 ); //this will floor

      LONG round = ( 5 + 3 ) - (( 5 + 3 ) % 3 ); //this should ceil

      This should round it... All 5's and 3's should be replaced by variable names... You should replace 5 + 3 with another variable name so it will look good.


      Nibu thomas Software Developer Programming Tips

      1 Reply Last reply
      0
      • R Remco Hoogenboezem

        I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3. I wrote the following code to do this: lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; In this piece of code lMinimum is de value that gets rounded up. And lIncrement is the value to wich lMinimum is rounded up. So if we choose the numbers mentioned above we get 6. This works fine for positive numbers but when we choose a negative number for lMinimum it goes wrong. I wrote another piece of code to handle negative numbers lCurrent=abs(lMinimum%lIncrement)+lMinimum; So combining the two we get: if(lMinimum>=0) { lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement; } else { lCurrent=abs(lMinimum%lIncrement)+lMinimum; } This is the obvious way and it contains one conditional branch. I was wondering of someone came accros a bit twiddling hack to do this more efficient. Thanks ;)

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #3

        Remco Hoogenboezem wrote:

        I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3

        Funny but it works Suppose your number is 5 and round it with multiple of 3

        LONG iNum=5;
        LONG iMultipleof=3;
        LONG iResult;
        iResult=(iNum/iMultipleof)*iMultipleof;

        Try it!:( But it works for less rounding Knock out 't' from can't, You can if you think you can :cool:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups