views:

118

answers:

3

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.

A: 

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

Meiscooldude
+8  A: 

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

scomar
+2  A: 

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.

Rotsor