增加 精確度 的算法

  1 #include <stdio.h>                                                                                                                                                                         
  2 
  3 int main()
  4 {
  5         int a = 10;
  6         int b = 3;
  7         int c = 4;
  8         int result = 0;
  9 
 10         result = (a/b)*c;
 11         printf ("result1 : %d \n", result);
 12 
 13 
 14         result = 0;
 15         result = (a*100/b)*c/100;
 16         printf ("result2 : %d \n", result);
 17         return 0;
 18 }
result1 : 12
result2 : 13

ex :

Precondition :
R_CHARGER_1 是 int type
R_CHARGER_2 是 int type
val 是 int type

val = (((R_CHARGER_1+R_CHARGER_2)*100*val)/R_CHARGER_2)/100;
為什麼要乘100再除100。

人的算法

(10/3)*4
10/3=3.3333333
3.333333*4=13.3333333333

電腦1

(10/3)*4
10/3=3
3*4=12

電腦2

((10*100)/3)*4/100
1000/3=333
333*4=1332
1332 / 100=13 <--- 結果最接近

posted @ 2017-04-21 17:23  wangchangruei  阅读(269)  评论(0编辑  收藏  举报