1

I noticed that some float variables are being shown wrong in Visual Studio 2017 debugger, although the internal value is right, that is, it is only wrong in the debug view.

For example:

obj_carro.vel1000rpm.push_back(stof(parte[2].substr(0, pos)));

cout << stof(parte[2].substr(0, pos)) << endl;
cout << obj_carro.vel1000rpm[obj_carro.vel1000rpm.size() - 1]  << endl;

It shows the correct value:

9.9
9.9

But VS 2017 debugger shows:

9.89999962  float

What is wrong? How to solve this?

Update: the cout output rounds to 6 decimal places after the point. This should be an answer, but the question is on hold...

Rogério Dec
  • 801
  • 8
  • 31
  • 5
    No, they are being shown "right" in the debugger, and truncated when output in your program. The answer is to fix the formatting in your program, if you want the "right" answer, and to read up on how floating point variables are implemented - start at http://floating-point-gui.de/ –  May 30 '18 at 19:01
  • 1
    The real question is why the `cout <<` result is not same as what OP sees in the debugger? Is `cout <<` rounding somehow? –  May 30 '18 at 19:01
  • 1
    @Arkady, yes it is. –  May 30 '18 at 19:02
  • 1
    @Arkadiy iostreams' default precision is 6 digits – kmdreko May 30 '18 at 19:03
  • @vu1p3n0x I found some references to that, yes. Where is it specified? –  May 30 '18 at 19:06
  • @Arkadiy in the iso c++ standard presumably. I don't know the standard and don't care to look for it, if you'd like to be sure you can ask a separate question and tag `language-lawyer` for an appropriate answer – kmdreko May 30 '18 at 19:11
  • Reference to default of `6` here: http://en.cppreference.com/w/cpp/io/ios_base/precision also here: http://en.cppreference.com/w/cpp/io/basic_ios/init – Richard Critten May 30 '18 at 19:37
  • https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Jesper Juhl May 30 '18 at 20:15
  • So the answer to this question is: *default precision for float is 6, here is why*. How is `[language-agnostic]` "Floating point math is broken" a duplicate? –  May 30 '18 at 20:29
  • 1
    @Arkadiy Because the question simplifies to: _"why can't the computer hold 9.9 exactly"_. – Richard Critten May 30 '18 at 20:52
  • @RichardCritten, I am arguing that it's not the case here, that this question is about rounding in `cout`. Of course I may be wrong. Maybe OP should clarify, if he wants the question reopened. –  May 30 '18 at 20:55
  • Added another answer to the dup. @RichardCritten, let me know if you'd rather own it. –  May 30 '18 at 21:06
  • The fact is, I've learned that a simple command like `float f = 9.9` will never store exactly `9.9` and that's sad, although the huge text indicated by @Neil Butterworth has millions of technical reasons for it. I also learned that `cout` and other commands round float result, never showing their exact value. But we have to adapt to reality and deal with it. – Rogério Dec May 30 '18 at 21:07
  • @RogérioDec At least the cout precision can be adjusted. –  May 31 '18 at 12:47

0 Answers0