Quote: "Of course, neither of those solutions work correctly (and the original question wasn't quite right either - what's half of 65535?)."
You're right about the solution being wrong, as it concentrates on start/end points, and misses the fact that we're talking about inclusive ranges. -100 to 100 has 201 integers in it, and 0 to 65535 has 65536 integers in it - (endpoint - startpoint + 1).
Here's working code, that's not quite right yet:
print Adjust( 0, 0, 65535, -100, 100)
print Adjust(32000, 0, 65535, -100, 100)
print Adjust(32767, 0, 65535, -100, 100)
print Adjust(33000, 0, 65535, -100, 100)
print Adjust(65535, 0, 65535, -100, 100)
wait key
end
function Adjust(Value as integer, LowIn as integer, HighIn as integer, LowOut as integer, HighOut as integer)
local InRange as integer
local OutRange as integer
local Ratio as float
InRange = (HighIn - LowIn) + 1
OutRange = (HighOut - LowOut) + 1
Ratio = (OutRange + 0.0) / InRange
Value = ((Value + LowIn) * Ratio) + LowOut
endfunction Value
The problem is that the rounding isn't quite right just yet - it needs to round to the nearest integer rather than truncating as it does now