-3

I just upgraded my computer and had to reinstall Visual Studio 2019, and I realized for some reason I can't work with float values. For example if I do a cout << (3/2); , it will return 1. I've tried looking into the float.h file but I don't know what to change. I guess it's a simple fix, I tried to google it but I didn't find a solution yet

Nysek
  • 1
  • Because 3 and 2 are both integers. You need to make at least one of them a floating point for the calculation to happen as a floating point. – drescherjm Mar 14 '21 at 01:55
  • Does this answer your question? [Integer division always zero](https://stackoverflow.com/questions/9455271/integer-division-always-zero) – ChrisMM Mar 14 '21 at 01:58
  • 1
    Adding on to what others have said, this has nothing to do with reinstalling Visual Studio. This is just how integer division works in C++. – TheUndeadFish Mar 14 '21 at 03:08

1 Answers1

2

You did integer division, so you got an integer back. If you tried std::cout << (3.0 / 2.0);, you'd get a double.

If you want floats: std::cout << (3.0f / 2.0f); gets you there.

How you type your literals matters in C++.

sweenish
  • 4,793
  • 3
  • 12
  • 23
  • Thanks a lot ! I was working with Visual Studio 2017 before and it was working fine, strange :/ – Nysek Mar 14 '21 at 12:18
  • VS 2017 would have treated the code you posted exactly the same. Something else (that you wrote) changed. – sweenish Mar 14 '21 at 14:02