Jump to content
  • Advertisement
Sign in to follow this  
rakoon2

generic getNearestStep function ?

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys! How would you guys make such a function? ea:
  Stepsize:           3
  number we insert:   7
 
  --> 3 6 9
         7
  The answer is 6.

==============================

  or:
  Stepsize:           16
  number we insert:   45

  --> 16 32 48
           45
  The answer is 48.  
    
==============================

  Stepsize:           3.333
  number we insert:   11.7 

  --> 3.333 6.666 9.999 13.332
                      11.7
  The answer is 13.332.   


Something like this:
template < typename T >
T getNearestStep( T number, T step_size )
{
  if( number % step_size == 0 )  // hmm mod only for int... not good
    return number;
 
   //.. 
}




Thanks! :) [Edited by - rakoon2 on September 25, 2004 12:07:28 PM]

Share this post


Link to post
Share on other sites
Advertisement
You just want arbitrary rounding it looks like
My solution Here is pretty good, it even has an optional base argument.
The function returns a number that is representable as base + integer * precision.

Share this post


Link to post
Share on other sites
Thank you!

This one isn't great:

template<typename T>
T getNearestStep( const T num, const T step_size )
{
const T answer = floor( (num + step_size/2) / step_size ) * step_size;
return answer;
}






1. Doesn't work with -values
2. floor.. I want a generic version. I don't want to write an own floor() version for every datatype.

Any Ideas?

Thanks! :)

Share this post


Link to post
Share on other sites
Sorry, your examples didn't contain negative values so I didn't think it was important.

1. if num < 0 then subtract step_size/2 instead of adding it.

2. You can just cast to int instead of calling floor().

3. How generic do you need this to be? Isn't just a few special versions for a few common data types good enough? (double, long) I doesn't seem like a common thing to do, and it's usefulness isn't immediately obvious.

Share this post


Link to post
Share on other sites
Well, I'll just write my own floor() function then. :)
Thank you for your help~


// answer = floor((num + stepsize/2) / stepsize) * stepsize;
template<typename T>
T getNearestStep( const T num, const T step_size )
{
if( num > 0 )
{
const T answer = floor( (num + step_size/2) / step_size ) * step_size;
return answer;
}
else
{
const T answer = ceil( (num - step_size/2) / step_size ) * step_size;
return answer;
}
}


[Edited by - rakoon2 on September 26, 2004 4:17:39 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!