 Feature Articles
 CodeSOD
 Error'd
 Forums

Other Articles
 Random Article
 Other Series
 Alex's Soapbox
 Announcements
 Best of…
 Best of Email
 Best of the Sidebar
 Bring Your Own Code
 Coded Smorgasbord
 Mandatory Fun Day
 Off Topic
 Representative Line
 News Roundup
 Editor's Soapbox
 Software on the Rocks
 Souvenir Potpourri
 Sponsor Post
 Tales from the Interview
 The Daily WTF: Live
 Virtudyne
Admin
Well, on some systems it pays of to do perform some seriously convoluted bithacks (and other workarounds) to avoid unpredictable branches. Even an extra modulo may well be far cheaper if you're unlucky.
But instead of an extra modulo wouldn't it be easier (and more natural? I honestly have no idea what the modulo solution does) just to add the signed bias to transform things into an unsigned range, and then subtract it afterwards to get a signed value back out.
value = INT_MIN;
value += step;
value = (unsigned) value % step;
value += INT_MIN;
Admin
Admin
I _did_ rethink that  negative modulus in valid:
c:\>irb
irb(main):001:0> 2%5
=> 3
Admin
The clue to this code is the value += 1 preceding the loop, which indicates that the author thought was using integer math and that value may be divisible by step already. In fact, in the submission Ward implies it is part of a loop, which I neglected to mention. The intention for that code was for ( int value = 0 ; value < n ; value += step ) { ... }. The comment was left unchanged.
Admin
Like all developers .. nobody has read the specs The fix would be as short as the following:
Admin
The original programmer wasn't stupid (well he was, but for other reasons) The original programmer didn't know how big value would be.
For example, I'm working on a program the displays text files. I can display so many lines per page, say 25... So my step is 25. If I read it a large 10,000 line program, then my value is 10,000. But if I read in a small 10 line program my value is 10. I didn't MAKE the variable smaller than step, it just is. Unforuntately us programmers have to deal with things like that.
You also said:
you are half correct. If the step is larger than the value the answer is a multiple of step (guess what, step is also a multiple of step AND if the value is LARGER than step, the answer BETTER not be step, cause well step is smaller than value so it can't be the next value divisible my step) If the answer is NOT larger than the value you do not need to do any voodoo magic, the answer will STILL be a multiple of step.
WHY or WHY would you want to think of, code and maintain a 7 line, multiple if solution when a nice succient 1 line solution works?
Admin
Most of the simple additions with modulo to solve the first problem will give the wrong answer in the overflow case when the next divisor of step greater than value would be greater than MAX_INT.
The the second answer, what if value is being passed in as an object and the function was trying to match anything where the object's toString method started with a ""?
Admin
And what exactly would be the correct answer in that case?
Yeah, I always use the term "negative" to describe an object whose toString method starts with a "". For example " you're an idiot" is a negative string...
Admin
Hahahahaha! Took me a minute and then I nearly choked laughing...
Admin
Well that depends, actually. In some languages/implementations, you get a definition of "remainder after truncated division" in which case (2 % 5) is 2, because 2 / 5 is 0. However, in some languages/implementations you get a much more mathematically sane (if sometimes less handy) definition which has (x % n) always vary between 0 and n1, in which case (2 % 5) is 3. 7+3 is 10, and you win. :)
Captcha: chocobot. Is that a bot that dispenses chocolate, or a huge robotic yellow bird?
Admin
abs(x) is your friend.
Catchpa: chocobot.
Admin
WTF! Everyone knows that the proper way to test if a number is negative is: take its square root. If the program fails, then the number is negative.
Admin
Actually, that's what I thought when I read it. When I was programming in BASIC at about 9 years of age I never thought of checking to see if a number was less than 0 to see if it was negative. Instead I used to check to see if x <> abs(x)
Admin
hahahahah this THREAD is the biggest WTF ever.
somebody needs to compile all the solutions, comment them and put it on the front page. the list would include:
poster names and count of errors they made
who first included the case when step is larger/smaller than value (no consequence at all)
captcah: 1337
Admin
But what is the next number divisible by 4 which is greater than 4? Look at the original code. If we assume it's doing the right thing, then it increments by 1 before testing the modulus. Therefore, in your example, 8 is the value required.
Admin
Shouldn't // find the next "value" divisible by "value"
be //find the next "value" divisible by "step"
and another point how can you find next divisible number divisible by step with value += step?
I have not think about it, but is not the code above simple? Are there another way?
Admin
I am so lazy.........................
Next_Value_Div_By_Step = Value + (Max(Value, Step)  Min(Value, Step))%Step
Admin
Admin
I just submitted this thread to Alex.
Anonymized, of course.
Admin
Three WTFs:
#1: The original coder already knows the % operator, but not how to use it.
#2: Many commenters don't either.
#3:
Prematurely microoptimizing nitwit. Did you even test this on your compiler, let alone all C compilers?
Admin
Indeed. What a waste of words.
The answer is always a multiple of step. (Where step, 0 and step are multiples of step, as anyone who has basic mathematical education will confirm.)
Yes, writing special case code for a special case that is covered by the general case makes a lot of sense. NOT.
No, it's not. First, there is no "lowest common denominator", unless you mean the negation of the greatest common denominator. Second, what you actually wanted to refer to is the least common multiple, which would be step * value / gcd(step, value), better written as step / gcd(step, value) * value in Cish languages. Third, you got the definition of the LCM wrong, too. Fourth, even the correct version does not have the slightest little thing to do with what the original code is trying to accomplish.
In other words: Sorry, not even close.
Admin
It was implementationdefined in original K&R C, but it's long since been standardized as roundtowardzero. The problem is that roundtowardzero is utterly useless behavior. You almost always want round toward negative infinity for integers, which would make % be a true modulus operator. It might be forgivable if there was a div() function that worked right, but there isn't. So you have two ways to do the wrong thing and no way to do the right thing (efficiently  there's still ((A % B + B) % B) available.)
This is one of the big things I'd fix if I had a time machine to go back and yell at early computer scientists. (For what it's worth, I'd also tell the ASCII committee to define a newline character. Not LF, not CR, not freaking VT, NEWLINE.)
Admin
Before anyone else yells at me... yes, I know this is wrong. I actually looked it up before posting to make sure I wasn't full of crap, discovered I was full of crap, and then forgot to fix it before hitting 'post'. Oops.
Admin
How about rounding towards zero for floating point to integer conversion. Would you like to change that too?
Frankly I've rarely had any problems with roundingtowardszero aside from the very fact that it's behavior is undefined, which can be especially nasty when dealing with deterministic code.
My personal pet peeve is that we're routinely told that optimizing compilers are "smart enough" to turn division and modulo by powers of two into right shifts and bitwise ands, which is clearly impossible when rounding towards zero (assuming that one's complement machines are finally extinct anyway).
Admin
In a mathematically sane world 2 % 5 should return an equivalence class consisting of [,,,7,2,3,8,13,,,] which are all equivalent in modulo 5. Alas, that's a bit hard for computers to work with and no where near as useful.
Admin
Isn't it just as simple as (C style, for ints)
value = ( (value / step) +1) * step;
?
of course, you'd need to do all the gubbins about making sure step wasn't zero etc. but I assume in this case that would be done in another Fn.
Captcha  stfu
Wonder if it's trying to tell me something?
Admin
Oops, missed a bit. That was assuming you would be able to modify one or other of the values outside the 'utility' function. If you can't do that, just store it somewhere and increment it by step.
Admin
I was looking for one of those "if ( (!(bool)varBool==true) != (false ^^ true))..." gems to add one snippet... But this thread is not worse for the case!
http://bash.org.ru/quote.php?num=66390
if (b.ToString().length < 5){...}
~~ Captcha: search the frying universe
Admin
i even think that the best practice would be to make a fusion with http://thedailywtf.com/Articles/How_Not_to_Parse_Command_Line_Arguments.aspx
Compare that dull, showing 100% lack of imagination and creativity, "if (b)" with the secure cryptographic "if (!(getHashValue(b.ToString().toUpperCase().toCharArray())!=xxx))"
Admin
I really can't believe the silliness of some of these comments. How to find the next value divisible by step? Here goes:
/* Find next value divisible by step. Assumes value is already divisible by step: */ value += step
Wow, that was profoundly difficult, no?
But what if value is not already divisible by step? A simple trick of integer math gives the solution which works in all cases:
value = (value / step + 1) * step
Notice that no stupid remainders are necessary. Learn how to use integer math, people.
Admin
Hm, not that right, I think.
value%step is not the distance to the next number. It is the distance from the previous number divisible by step. Look at for example: value = 7, step=5. value%step = 2, since 7 = 1*5 + 2. And 7+2=9 is not divisible by 5, but 72=5 is.
Admin
If you want the next Highest value divisible by step, you can do this: Value = (Ceiling(Value/step) * step).
Implement your own Ceiling function or use a runtime one, your choice.
Some of these proposed "solutions" are really off the mark unless I am misunderstanding something.
Test Inputs: Value=20 Step= 3 Output = 21
Admin
I don't know if this was posted already, but:
value += stepvalue%step
value = 11 step = 5
value%step = 1
step1 = 4
value+4 = 15
People these days..