1. ## closest value

Say you do a series of calculations and come up with a value.
now you dont want to display the calculated value but you want to check to see which of the following values it is closest to: 1/4000, 1/2000, 1/1000, 1/500, 1/250, 1/125, 1/60, 1/30,
1/15, 1/8, 1/4, 1/2, 1, 2, 4, 8, 15, 30, 60

how do you do that in C

Also can some one please tell me how to demonstrate that in psuedocode??

thanks

2. Draw it on paper?
Code:
`a------x------b---------c-----d----e`
Which is x closest to? The shorter distance: a-to-x or x-to-b, etc.

3. but how do u do that in C??

4. Subtract.

You've got list of positions to compare with a current position. Go through the list and find the smallest difference.

5. thanks dave but i realise i have to do that, i can even do it for one value but i dont know how to compare the whole list and find the smallest difference in C.

i dont mean you to do it all for me, just need more help e.g. what command line do i use???

6. You've got some hints for a start, please post your initial attempt.

Are you familiar with arrays? Loops? Functions?

7. Code:
```#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (int argc, char * argv)
{
// Mainline Variable Declarations
FILE * output = stdout;
FILE * input = stdin;

IF value-[1/400,...etc]=0
insertvalue == 1
ELSE

//i have no idea how to find the least difference```
something like that??

8. In a cycle you do:
Code:
```if (diff < currMinDiff)
{
currMinDiff = diff;
}```

9. Directly showing you the exact lines is not a good idea , but I can tell you the algoirth that came to my head firstly.

You can keep a control variable. Than you take the substraction of the number you want to compare and the number you have taken from the calculations you have made before. You put that into the control variable . In each loop you compare the control variable with the former one. If the new one is the shorter than the former you put the new one to the control variable.

10. It is probably worth considering that the values are non-linear. So you need to find the two values that are closest to your ideal value. Say you calculate 0.01 (1/100), but 1/100 isn't valid, it has to be either 1/125 or 1/60 - so now you need to see which of those is the "closest". Subtracting isn't really that good here either, you should probably use some sort of "proportional difference", e.g. the (y - x1) / x1 compared with (x2 - y) / x2, where x1 and x2 are the closest value above and below.

You could of course cheat - if the value is less than 1/60, and above 1/125, then pick 1/125 - that's most likely a good approximation - or some fixed ratio of "allowed overexposure", e.g. if you come up with a value pretty close to 1/60, say up to 10% "faster", you use 1/60, then go to 1/125.

--
Mats