C++ truncating float value [duplicate]

  Kiến thức lập trình

I’m completely new to programming and C++. I’m doing some practice exercises but I noticed when I add large numbers together the output value is truncated as if it’s an int data type even though both data types are float. Can someone please explain why this happens or if it’s something I’m doing wrong?

#include <iostream>
#include <cmath>

int main() {
    float num1 = 253654.69;
    float num2 = 362554.73;
    float num3 = num1 + num2;
    std::cout << num3;
    return 0;
}
// all variables are float type
// expected output = 616209.42 but value is being truncated = 616209

New contributor

3rd World Gamer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

LEAVE A COMMENT