You test that r[e] is not zero and divide by it below so I wonder if here you meant to divide by r[d] here.