Archived

This topic is now archived and is closed to further replies.

how to find closest point on a scale

This topic is 5152 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

lets say i have numbers 1 - 1024 stepping by a constant rate of 16 now lets say i have a number, such as 52 how can i find the closest match, mathematically, to the number within the scale for this specific example, the answer would be 49

Share this post


Link to post
Share on other sites
Well, let''s see. If you % your number by your step size, you get the remainder of your number divided by your step size. So if

int mod = input % step;

''mod'' in this case should be 4.

Then:

if (mod < step / 2)
output = input - mod;
else
output = input + step - mod;

Then I guess

output++;

since you''re going from 1 instead of 0.

So, altogether:

int Closest(int input, int step)
{
int mod = input % step;
int output;
if (mod < step / 2)
output = input - mod;
else
output = input + step - mod;
output++;
return output;
}

Now this was totally off the top of my head, so I could have easily overlooked something. But whether or not this is exactly what you need, maybe it will point you in the right direction.

Share this post


Link to post
Share on other sites