Click here to Skip to main content
15,889,595 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Sorting algorithm problem Pin
Wjousts21-Jul-11 9:02
Wjousts21-Jul-11 9:02 
GeneralData Pin
Burt12313-Jul-11 10:23
Burt12313-Jul-11 10:23 
GeneralRe: Data Pin
Richard MacCutchan13-Jul-11 21:48
mveRichard MacCutchan13-Jul-11 21:48 
GeneralRe: Data Pin
Burt12314-Jul-11 7:37
Burt12314-Jul-11 7:37 
GeneralRe: Data Pin
Richard MacCutchan14-Jul-11 8:11
mveRichard MacCutchan14-Jul-11 8:11 
QuestionModulo statement Pin
Lutosław5-Jul-11 1:44
Lutosław5-Jul-11 1:44 
GeneralRe: Modulo statement Pin
David19875-Jul-11 2:05
David19875-Jul-11 2:05 
GeneralRe: Modulo statement Pin
BobJanova5-Jul-11 2:38
BobJanova5-Jul-11 2:38 
I agree that the question was probably meant to constrain to ±6. I would use ((x+18)%12)-6 because mod with negative numbers doesn't do what you expect, in some languages at least.
Edit: that sentence was rubbish, the number needs to be brought into [-18,] before that is useful.

This has a potential overflow if x can be near the top of the range for the numeric type in question. To avoid that you can do something like
x = x % 12; // -11 to +11
if(x < 0) x += 12; // 0 to +11
if(x > 6) x -= 12; // -5 to +6


(You can one line it: x = (((x%12)+18)%12) - 6 ... but two mods is likely to be slower than the branched version. You can also one line the branches:
x = (x = (x = x % 12) < 0 ? x + 12 : x) > 6 ? x - 12 : x;

... but your manager is likely to shoot you and it is no faster.)
GeneralRe: Modulo statement Pin
David19875-Jul-11 3:13
David19875-Jul-11 3:13 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 2:47
Lutosław5-Jul-11 2:47 
GeneralRe: Modulo statement Pin
David19875-Jul-11 3:06
David19875-Jul-11 3:06 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 5:19
Lutosław5-Jul-11 5:19 
GeneralRe: Modulo statement Pin
BobJanova5-Jul-11 5:45
BobJanova5-Jul-11 5:45 
GeneralRe: Modulo statement Pin
David19875-Jul-11 6:03
David19875-Jul-11 6:03 
GeneralRe: Modulo statement Pin
BobJanova5-Jul-11 7:13
BobJanova5-Jul-11 7:13 
GeneralRe: Modulo statement Pin
BobJanova5-Jul-11 3:13
BobJanova5-Jul-11 3:13 
GeneralRe: Modulo statement Pin
David19875-Jul-11 3:14
David19875-Jul-11 3:14 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 5:13
Lutosław5-Jul-11 5:13 
GeneralRe: Modulo statement Pin
BobJanova5-Jul-11 5:43
BobJanova5-Jul-11 5:43 
GeneralRe: Modulo statement Pin
David19875-Jul-11 6:19
David19875-Jul-11 6:19 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 6:46
Lutosław5-Jul-11 6:46 
GeneralRe: Modulo statement Pin
David19875-Jul-11 6:53
David19875-Jul-11 6:53 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 7:24
Lutosław5-Jul-11 7:24 
GeneralRe: Modulo statement Pin
David19875-Jul-11 7:40
David19875-Jul-11 7:40 
GeneralRe: Modulo statement Pin
Lutosław5-Jul-11 10:47
Lutosław5-Jul-11 10:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.