Check for NaN instead of doing a float conversion

Signed-off-by: Jonas Kalderstam <jonas@kalderstam.se>
Catch exception

Signed-off-by: Jonas Kalderstam <jonas@kalderstam.se>