相关疑难解决方法(0)

处理浮点数中的精度问题

我想知道是否有办法克服精确度问题,这似乎是我的机器内部表示浮点数的结果:

为清楚起见,问题归纳为:

// 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的理想结果还差不多.

任何建议最受欢迎.

c++ floating-point floating-accuracy

9
推荐指数
5
解决办法
1万
查看次数

0.699 x 100 = 69.89999999999999?

可能重复:
为什么99.99/100 = 0.9998999999999999
处理浮点数的精度问题

我在php和javascript中看到过这个问题.我有这个号码:float 0.699

如果我这样做:0.699 x 100 = 69.89999999999999

为什么?

编辑

round(0.699 x 10, 2):float 69.90000000000001

javascript php math decimal floating-accuracy

1
推荐指数
2
解决办法
516
查看次数

用整数除以double

我正在面对一个问题,而将一个分为double一个int.代码段是:

  double db = 10;
  int fac = 100;
  double res = db / fac;
Run Code Online (Sandbox Code Playgroud)

res的值0.10000000000000001不是0.10.

有谁知道这是什么原因?我正在使用cc来编译代码.

c floating-point precision floating-accuracy

0
推荐指数
1
解决办法
2万
查看次数