我想知道是否有一种克服精度问题的方法,这似乎是我机器内部浮点数表示的结果:
为了清楚起见,问题总结如下:
- // str is "4.600"; atof( str ) is 4.5999999999999996
- double mw = atof( str )
- // The variables used in the columns calculation below are:
- //
- // mw = 4.5999999999999996
- // p = 0.2
- // g = 0.2
- // h = 1 (integer)
- int columns = (int) ( ( mw - ( h * 11 * p ) ) / ( ( h * 11 * p ) + g ) ) + 1;
在转换为整数类型之前,列计算的结果为1.9999999999999996;所以距离2.0的预期结果还远.
任何建议最受欢迎.