0

I am trying to set double variable in Visual Studio 2019 with (c++17) but after compilation in debug mode I see another value but not as expected. Why? for example

double v1 = 1.161050;
double v2 = 1.161190;

I added breakpoint and see next values:

v1 = 1.1610499999999999
v2 = 1.1611899999999999
Pasha
  • 89
  • 9
  • 3
    Handy reading: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – user4581301 Oct 26 '21 at 19:34
  • 1
    You've run into a representation problem. A `double` on your system cannot represent 1.161050. The best it can do is give you a close enough 1.1610499999999999. – user4581301 Oct 26 '21 at 19:35
  • Close enough is not good, I need exactly the same value I set. How to make it correct? – Pasha Oct 26 '21 at 19:42
  • 1
    @Pasha Don't use `double` if you require exact numbers. You can use an integer type with the convention that the number is implicitly divided by 1000000. aka Fixed point. –  Oct 26 '21 at 20:07
  • The fundamental reason is that not every decimal in the mathematical sense can be accurately represented in a finite number of digits in binary. – Yujian Yao - MSFT Oct 27 '21 at 07:45

0 Answers0