There is one thing to clarify when mapping a float interval to an integer interval, namely how the boundaries of the float interval shall be mapped to the boundaries of the integer interval.
Taking the example of the float interval [-1.0, +1.0], assuming here, that the endpoints of the interval can actually occur. If the target integer interval is 0..319: Which floating point interval shall be mapped to 0?
If we linearly map [-1.0, +1.0] to [0.0, 319.0] then this gives (as others have explained) the following formula: y = 159.5x + 159.5.
When rounding is done to the nearest integer this will have the following effect:
- float numbers [-1.0, −0,996865204] will be mapped to 0. This interval has a width of 0,003134796.
- float numbers [−0,996865205, −0,990595611] will be mapped to 1. This interval has a width of 0,006269593.
- float numbers [0,996865204, +1,0] will be mapped to 319. This interval has a width of 0,003134796.
You see that the ranges of float values that map to 0 and 319 are only half as wide as the ranges of float values that map to numbers from 1..318. This may be exactly what you want, or it may be acceptable, in which case the formula y = 159.5x + 159.5 is perfectly fine.
For other rounding strategies (round towards minus infinity, round towards plus infinity) the results will be different, but there will still be an asymmetry with respect to the width of the intervals for some the numbers.
If instead you want to achieve that for each target integer number the source intervals shall be equally wide, then you would have to calculate the mapping from [-1.0, +1.0] to [-0.5, 319.5). This would result in the formula y = 160.0*x + 159.5 with rounding, giving the desired numbers from 0..319, plus a special case handling of +1.0 which would have to be mapped to 319 rather than 320.