Mapping a range of numbers to another range of numbers

Hi all ... had to convert a range of numbers in my online course to another range of numbers using C code ... 

in Arduino this is easy using the Map function ... but i had a lot of problems trying to figure out to do it in C and after googling online and not finding anything of use decided id have to try harder doing it myself so got out the excel sheet and tryed working it out myself ... and after getting it sorted decided to do a little write up on it .... 

ok so was simulating a reading in one of my final labs and it took readings in from 0 to 4095 these numbers then had to be converted to a distance ranging from 0.000 to 2.000 cm in that format 

so my first attempt which worked was dividing the reading by 2 and then checking the (%)modulo of the number and taking that from the reading divided by 2 .... seemed to work but got to a point fairly quickly where readings were out eg ok to 409 but above that were wrong ... had to use the same formula for the next set of numbers but also take 10 from the result eg

while (sample >859  && sample <=1269){             // the high number is where i worked out things changed

 d=sample%10;

Distance = ((sample-d)/2)-10;                    // here i did function and had to deduct 10

return Distance;

kept going like this hit anothe range had to deduct  15 next range deduct 20 utill finally last range deduct 45 

/while (sample >3849 && sample <=4097){

d=sample%10;

Distance = ((sample-d)/2)-45;

return Distance;

 

which gave me a total of 10 while loops in the function to cover all the numbers ..... But it worked it mapped all the readings so eg if reading was 40 i got 0.020 cm or if reading was 1187 i got 0.580 cm  .... reading 2457 i got 1.200 cm

But i still felt it should be simpler than doing 10 while loops (could have done if statements but i like while loops)

so left it and came back tonight and looke at all the numbers again ... saw my range in cm went from 0 to 2.000 in 0.020 jumps giving 100 seperate readings ... so got my excel to show me 0 to 4095 in 100 seperate jumps and went up in  jumps of 40.95

then looked at the difference of the reading to what i needed eg if reading was 4095 and i needed 2.000 cm difference was 4095/2

which is 2047.5 so difference was 47.5 if i lookded at other end and readin was 81.9  which means /2 gives 40.95 i needed 40 so difference was .95 

i then wanted to see if there was any realtion between all these numbers eg were they similar precentage wise so looked at my last one eg 4095 and divided it by 2 again to get 2047.5 and i divided this by the amount extra 47.5 and came back 43.10526 

went to one of my start numbers eg 81.9 divided by 2 got 40.95 and divided this by the extra .95 and yes got 43.10526

so i had found a number that didnt vary unlike my previous equations with all the while loops .... and created a new formula that i hoped would work ...

                d=(sample/2)/43;

output = (sample/2-d);

 Distance = output;

return Distance;

and it did except for 4 readings .... all of which ended with a 9 .... so back to excel and looked to see why this happened and found this  reading 3357/2 =1678.5    ... 1678.5 /43 =39.0348     .... 1678.5 - 39.0348 = 1639.9  ....  so i didnt get the 1.640 i should have had ....and here was the problem it didnt round up ... so decided to look at other numbers and found similar which it did round up so was a bit strange that for 4 it did not 

so decided to see could i add code to my code to fix this .... and i did i went with a modulo check and it worked ... if a number ended with anything other than a 0 i added 1 and bingo problem sorted and this is what my code ended like

            d=(sample/2)/43;                                          

output = (sample/2-d);

out = output %10;                                //    gets modulo of number

if (out > 0){                                         //if it has a modulo add 1    

Distance = output + 1;

return Distance;

}

else  Distance = output;

return Distance;

 

Have to say much preferr this 8 lines of code to my previous 10 while loops ( which can be seen here https://www.robotshop.com/letsmakerobots/lets-learn-basics-robotics-with-edxs-embedded-systems-shape-the-world#comment-117599)

anyway it was great to be able to figure out how to do this dont know if its of any use to anybody but i thought it was a interesting problem with a interesting solution on how i reached it.