0

I am trying to pass double declared as a const to a function.

const double temp = 0.0007;
compute(temp); // calling compute function with double as argument

compute function is declared as compute(const double&)

    When i test it the double value gets converted to **0.00069999999999999999** 

compute (this=0x8173870, max_warpage_limit=@0xdaf450: 0.00069999999999999999,  at test.cpp:283

Precision is very crucial for me because I am using this value in calculations and this deviation is not acceptable. Can experts here please share their opinion on what's happening? Looking forward to your views

suresh
  • 115
  • 7
  • 4
    *Precision is very crucial for me because I am using this value in calculations and this deviation is not acceptable* -- The constant 0.0007 cannot be represented exactly in binary. So you're already lost on the very first line of your code. – PaulMcKenzie Nov 02 '18 at 22:04
  • If the reason why you need these values to be exact is to do financial/monetary calculations, don't use `double`. Either use integers, or get a library that uses types that does "exact math". – PaulMcKenzie Nov 02 '18 at 22:08
  • 1
    *Can experts here please share their opinion on what's happening?* -- There are no opinions -- that's the way it is with binary floating point. You can try [this link](https://floating-point-gui.de/). – PaulMcKenzie Nov 02 '18 at 22:13

0 Answers0