Look at the function that does that at
http://pulua.hcc.hawaii.edu/files/mapper.c
...or ask Dave Raggett, I believe he's tacking that code at the
end of the HTML+ DTD in an appendix. The function was worked on by a man
who does some heavy computer graphics work - he's analyzed many such functions,
and this one has proven itself to be one of the best. It has been featured
in the online "Raytracing News", and it is publicly distributable.
And one nice thing is, you don't need anything above integer math
for it.
-- Kevin