Dividing a double with integer

↘锁芯ラ 提交于 2020-01-03 05:43:05

问题


I am facing an issue while dividing a double with an int. Code snippet is :

  double db = 10;
  int fac = 100;
  double res = db / fac;

The value of res is 0.10000000000000001 instead of 0.10.

Does anyone know what is the reason for this? I am using cc to compile the code.


回答1:


You need to read the classic paper What Every Computer Scientist Should Know About Floating-Point Arithmetic.




回答2:


The CPU uses binary representation of numbers. Your result cannot be represented exactly in binary. 0.1 in binary is 0.00011001100110011... CPU truncates it at a certain point and gets some rounding error.




回答3:


double is a floating point operator, they do not provide precise values. Look up Precision and Floating Point Operators on google.



来源:https://stackoverflow.com/questions/3051135/dividing-a-double-with-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!