Fix a bug in numerical differentiation, refactor it and improve its docs.