# 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.

## Recommended Posts

Hello guys! How would you guys make such a function? ea:
  Stepsize:           3
number we insert:   7

--> 3 6 9
7

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

or:
Stepsize:           16
number we insert:   45

--> 16 32 48
45

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

Stepsize:           3.333
number we insert:   11.7

--> 3.333 6.666 9.999 13.332
11.7


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 on other sites
Do you want the number to go to the next highest step?

The first one should be 9 then not 6.

##### Share on other sites
Nope. To the nearest! :) -> the first one is right! ;-)

##### Share on other sites
Something like this?:
answer = floor((num + stepsize/2) / stepsize) * stepsize;

##### Share on other sites
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 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 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 on other sites
Well, I'll just write my own floor() function then. :)

// 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]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

×

## Important Information

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!