Dynamic integer will be any number from 0 to 150.
i.e. - number returns 41, need to return 50. If number is 10 need to return 10. Number is 1 need to return 10.
Was thinking I could use the ceiling function if I modify the integer as a decimal...? then use ceiling function, and put back to decimal?
Only thing is would also have to know if the number is 1, 2 or 3 digits (i.e. - 7 vs 94 vs 136)
Is there a better way to achieve this?
Thank You,
In pseudo code:
In Python:
That should do it. Keep in mind that the above code will cast an integer to a float/double for the arithmetic, and it can be changed back to an integer for the final return. Here's an example with explicit typecasting
In Python (with typecasting):
How this works. The % operator evaluates to the remainder of the division (so
41 % 10
evaluates to 1, while45 % 10
evaluates to 5). Subtracting that from 10 evaluates to how much how much you need to reach the next multiple.The only issue is that this will turn 40 into 50. If you don't want that, you would need to add a check to make sure it's not already a multiple of 10.
tl;dr:
((n + 9) / 10) * 10
compiles to the nicest (fastest) asm code in more cases, and is easy to read and understand for people that know what integer division does in C. It's a fairly common idiom.I haven't investigated what the best option is for something that needs to work with negative
n
, since you might want to round away from zero, instead of still towards +Infinity, depending on the application.Looking at the C operations used by the different suggestions, the most light-weight is Mark Dickinson's (in comments):
It looks more efficient than the straightforward divide / multiply suggested by a couple people (including @bta):
((n + 9) / 10) * 10
, because it just has an add instead of a multiply. (n+9
is a common subexpression that only has to be computed once.)It turns out that both compile to literally identical code, using the compiler trick of converting division by a constant into a multiply and shift, see this Q&A for how it works. Unlike a hardware
div
instruction that costs the same whether you use the quotient, remainder, or both results, the mul/shift method takes extra steps to get the remainder. So the compiler see that it can get the same result from a cheaper calculation, and ends up compiling both functions to the same code.This is true on x86, ppc, and ARM, and all the other architectures I've looked at on the Godbolt compiler explorer. In the first version of this answer, I saw an
sdiv
for the%10
on Godbolt's gcc4.8 for ARM64, but it's no longer installed (perhaps because it was misconfigured?) ARM64 gcc5.4 doesn't do that.Godbolt has MSVC (CL) installed now, and some of these functions compile differently, but I haven't taken the time to see which compile better.
Note that in the gcc output for x86, multiply by 10 is done cheaply with
lea eax, [rdx + rdx*4]
to do n*5, thenadd eax,eax
to double that.imul eax, edx, 10
would have 1 cycle higher latency on Intel Haswell, but be shorter (one less uop). gcc / clang don't use it even with-Os -mtune=haswell
:/The accepted answer (
n + 10 - n % 10
) is even cheaper to compute:n+10
can happen in parallel withn%10
, so the dependency chain is one step shorter. It compiles to one fewer instruction.However, it gives the wrong answer for multiples of 10: e.g.
10 -> 20
. The suggested fix uses anif(n%10)
to decide whether to do anything. This compiles into acmov
, so it's longer and worse than @Bta's code. If you're going to use a conditional, do it to get sane results for negative inputs.Here's how all the suggested answers behave, including for negative inputs:
(transpose awk program)
Ignacio's
n + (((9 - (n % 10)) + 1) % 10)
works "correctly" for negative integers, rounding towards +Infinity, but is much more expensive to compute. It requires two modulo operations, so it's essentially twice as expensive. It compiles to about twice as many x86 instructions, doing about twice the work of the other expressions.Result-printing program (same as the godbolt links above)
or