use cmath's isfinite() for nan and inf check