Added overflow protection in the calculation of the logistic function