tuzhuke 发表于 2018-11-30 08:08:21

代码中做除法避免除0


为了管理方便,论坛是邀请制,如果需要注册论坛,点击论坛上方的“店铺”,旺旺联系免费获得邀请码


下面的代码,求多次测量的均值(中间除以1000 是将mm转换成m)
sprintf(dist_str, "an2:%3.2fm", (float)Anthordistance/1000/Anthordistance_count);
      OLED_ShowString(0, 6,"               ");
      OLED_ShowString(0, 6,dist_str);
但是可能遇到的问题是,有个anthor 根本没有回复数据,那么Anthordistance_count 可能是0,今天检查代码发现这个问题,之前程序竟然没有hang


下面才是正确做法,加一个判断
if(Anthordistance_count>0)
    {
      sprintf(dist_str, "an1:%3.2fm", (float)Anthordistance/1000/Anthordistance_count);
      OLED_ShowString(0, 4,"               ");
      OLED_ShowString(0, 4,dist_str);
    }

页: [1]
查看完整版本: 代码中做除法避免除0