Code snippets with member pointing error and better implementations is really cool, it could be just posted on general programming thou, a sub-forum on general programming perhaps? Or a sub-forum for each major tech forum (snippets for networking, directX, etc..)
I think it could work cause its really ego intensive, ppl will love to show how better they can do, and its beneficial for everyone. I always want to just post something just to ask if "is that the best way to do this", but feel is just too personal and abusive post.
For example, I just implemented rounding a number up to the next multiple of a number
int RoundUp( number, multiple ); I found quite a lot of implementations for this in stack overflow, not sure whats the best., mine ended like this:
inline uint32_t RoundUpToNextMultiple( uint32_t n_p, uint32_t multiple_p ){
return n_p + multiple_p - 1 - (n_p - 1) % multiple_p;
}
inline int32_t RoundUpToNextMultiple( int32_t n_p, int32_t multiple_p ){
if( n_p < 0 ){
// multiple_p = -multiple_p; rounds in the left direction (rounds down)
return - ((int)(RoundUpToNextMultiple( (uint32_t)-n_p, (uint32_t)multiple_p )) - multiple_p );
}
return RoundUpToNextMultiple( (uint32_t)n_p, (uint32_t)multiple_p );
}
The negative checks makes the round goes towards zero, so it always rounds to the right(->) direction
RoundUpToNextMultiple( -18, 16 ); // 16
RoundUpToNextMultiple( -2, 16 ); // 0
RoundUpToNextMultiple( 0, 16 ); // 0
RoundUpToNextMultiple( 2, 16 ); // 16
RoundUpToNextMultiple( 18, 16 ); // 32