fixed handling integer underflows and overflows

Last commit handles integer underflows and overflows by taking absolute values what is incorrect.

This commit uses averages between min and max values.