分母不能为0

对于int 类型,如果分母为0,在程序运行时,会报错。

而对于float 类型,如果分母为0,则不会报错,而是会返回一个infinity(无穷大),也就是NAN。

因为除一个无穷小的数,返回一个无穷大的值。

对于百分比的运算,因为一般在计算时,都会转成float,要注意这个坑。

示例

float percent = (float) current/ (float)total;

正确示例

float percent = 1;
if(total>0){
	 percent = (float) current/ (float)total;
}

相关文章:

  • 2022-12-23
  • 2021-04-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-05-08
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案