我想知道是否有办法克服精确度问题,这似乎是我的机器内部表示浮点数的结果:
为清楚起见,问题归纳为:
// 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;
Run Code Online (Sandbox Code Playgroud)
在转换为整数类型之前,列计算的结果是1.9999999999999996; 距离2.0的理想结果还差不多.
任何建议最受欢迎.
我在php和javascript中看到过这个问题.我有这个号码:float 0.699
如果我这样做:0.699 x 100 = 69.89999999999999
为什么?
编辑
round(0.699 x 10, 2):float 69.90000000000001
我正在面对一个问题,而将一个分为double一个int.代码段是:
double db = 10;
int fac = 100;
double res = db / fac;
Run Code Online (Sandbox Code Playgroud)
res的值0.10000000000000001不是0.10.
有谁知道这是什么原因?我正在使用cc来编译代码.