问题
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