Can't you check for this before calculating the exponents?