测试程序发现了一个bug, 几经调试最后定位到一条复杂表达式语句的计算结果不正确.

代码中用复杂表达式不是一个好习惯,调试起来很不方便.

为了方便跟踪调试,看看究竟是表达式的那部分计算出了问题.
就先把表达式拆开来写了.

再运行, 嗨, bug没有了. 跟踪看了表达式最终计算结果,也正确了.

问题表达式如下:
this.backgroundScaleFactor = ((this.fishEyeMagFactor * (this.LengthOfScale - (this.fishEyeHigh - this.fishEyeLo))) + (this.fishEyeHigh - this.fishEyeLo)) / (this.timeLine.VisibleTime(this.startTime, this.stopTime) * this.fishEyeMagFactor);

运行时得到的 结果不正确.
拆解为如下:
double tmp1 = ((this.fishEyeMagFactor * (this.LengthOfScale - (this.fishEyeHigh - this.fishEyeLo))) + (this.fishEyeHigh - this.fishEyeLo));
long tmplng = this.timeLine.VisibleTime(this.startTime, this.stopTime);
this.backgroundScaleFactor = tmp1 / (tmplng * this.fishEyeMagFactor);

 再运行结果就正确了.

教训: 尽量避免书写复杂表达式, 调试跟踪不方便.

问题的原因:
我估计是数据类型隐式转换造成的.

简单表达式的转换,很容易看清楚,可能有转换错误的一眼就能看出来.
复杂表达式自己都看得迷糊了.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2021-06-20
  • 2021-04-22
  • 2021-08-04
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2021-05-25
相关资源
相似解决方案