0

So i have this value that i simply want to "cout" it

double f = -922337203.6854775808;
std::cout.precision(19);
//cout.setf( std::ios::fixed, std:: ios::floatfield );
cout << f<< endl;

output : -922337203.6854776144

value changes here. Any information can help.

  • 2
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Algirdas Preidžius Jan 29 '21 at 09:00
  • That's the way it is with floating point numbers. Even simple assignments often do not work as expected... – U. W. Jan 29 '21 at 09:07
  • what is the solution then? – Mohamed Zaouia Jan 29 '21 at 09:18
  • There's no value change. Two things are happening, Firstly `-922337203.6854775808` has no exact representation so you don't have that exact value in the first place (instead you have the closest value that can be represented). Secondly there's no requirement that floating point numbers print accurately, so you can't look at your output and be sure that the output is exactly the same as the number you really have. – john Jan 29 '21 at 09:20
  • @MohamedZaouia There is no solution. Floating point numbers don't work the way you want them to. – john Jan 29 '21 at 09:20
  • "i simply want to cout it" Frankly I doubt it. If you are serious then the answer is "impossible". But if you could take one step back and discuss the problem you have with whatever you really want to achieve, then help and solutions might be available. I am implying that we are looking at a https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem In case you agree you could create a new question. – Yunnosch Jan 29 '21 at 09:30
  • humm, this is fun :(, thanks anyway – Mohamed Zaouia Jan 29 '21 at 09:31
  • i just need to cout, so thanks everyone – Mohamed Zaouia Jan 29 '21 at 09:32

0 Answers0