Rounding up to a multiple of a number
-
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 ;)
-
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 ;)
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
and3's
should be replaced by variable names... You should replace5 + 3
with another variable name so it will look good.
Nibu thomas Software Developer Programming Tips
-
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 ;)
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: