#### Archived

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

# how to find closest point on a scale

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

## 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 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 on other sites
Also (((value - 1) + step/2) / step) * step + 1.

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 10
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632942
• Total Posts
3009344
• ### Who's Online (See full list)

There are no registered users currently online

×