worked around g++-4.6 unsigned comparison warning
fixed computation of nmax for integral types smaller than int
added avoiding the signed integer overflow by making n negative