 Makistos
10/25/2013 - 12:30 PM

## This simple code snippet shows how interpolating can be done in C using signed integers. The for() loop determinest the range to interpo

This simple code snippet shows how interpolating can be done in C using signed integers.

The for() loop determinest the range to interpolate from. In this case -100 to +100. For loop is here just to work as an example.

12th line is were the work is done. 256.0 is the top end of the range while 201.0 is the number of values in the list to be interpolated (-100 to +100). 127 is added because we want to interpolate to values between 0 and 255, not -127 to +127. The other two lines are just for rounding.

The trick here is to use floating point values instead of whole numbers, otherwise interpolation won't work properly.

#interpolation #c

``````#include <stdio.h>
#include <math.h>

int main()
{
int result;
double x;
int i;

for (i=-100;i<101;i++)
{
x = (256.0/201.0*i +127);
if (x>=0) result = (x+0.5f);
else result = (x-0.5f);
printf("%d => %d\n", i, result);
}
}``````