How come map() function is proper and correct? What is the idea behind it?

J-M-L:
Here is a little challenge for you and your math background: please offer an integer result map function that behaves like you would want it to work, that guarantees that extreme values are mapped to each other (floating point might get you in trouble depending how you write this), that is not too much slower than the current one, and works with large range mapping and short range mapping as you explain.

Huh?
I already wrote that integer based map() function. See post #30
http://forum.arduino.cc/index.php?topic=417690.msg2877460#msg2877460

Not sure what you mean by "extreme values".

Run that map function in post #30 against the standard map() function with your favorite test cases.
And to make it even easier for you, here is a sketch to try it on actual Arduino h/w.
It will show you the full range of mapping and the differences between the original map() function and this new one.

All you have to do is modify the in and out range values and run it.

long newmap(long x, long in_min, long in_max, long out_min, long out_max)
{
	if( x == in_max)
		return out_max;
	else if(out_min < out_max)
		return (x - in_min) * (out_max - out_min+1) / (in_max - in_min) + out_min;
	else
		return (x - in_min) * (out_max - out_min-1) / (in_max - in_min) + out_min;
}

void setup(void)
{
long int val, valend, mval, mval2, cval, imin,imax,omin,omax;
char buf[80];
int incr;

	Serial.begin(9600);
	while(!Serial){}

	// fill in in & out ranges 
	imin = 1; imax = 100;
	omin = 1; omax = 3;

	if(imax > imin)
		incr = 1;
	else
		incr =-1;

	val = imin;
	valend = imax + incr;
	while(val != valend)
	{
		sprintf(buf,"map(%4ld,%ld,%ld,%ld,%ld) ",val,imin,imax,omin,omax);
		Serial.print(buf);
		mval = map(val, imin, imax, omin, omax);
		mval2 = newmap(val, imin, imax, omin, omax);
		sprintf(buf, "val: %4ld, map: %4ld, newmap: %4ld", val, mval, mval2);
		Serial.print(buf);

		if((mval != mval2))
			Serial.print(" <---map Diff");
		Serial.print("\n");

		val += incr;
	}
}
void loop(void){}

Now go try your favorite ranges.
Here are a few of my favorite interesting test cases:

1,100,1,3

0,1023,1,5

1,100,1,10

1,10,10,1

1,10,1,5

1,5,0,255

0,1023,0,1

You will notice a much smoother (better/correct) mapping and I would say it generates the mappings that people would expect.

--- bill