Question Details

No question body available.

Tags

c++ windows double c++20 mingw-w64

Answers (2)

Accepted Answer Available
Accepted Answer
August 16, 2025 Score: 13 Rep: 36,828 Quality: Expert Completeness: 70%

As you can see in std::iosbase::precision documentation:

The default precision, as established by std::basic_ios::init, is 6.

This is the reason you see 339818 as the output in the first snippet:
std::cout will use a default of 6 digits, and so 339817.5 is rounded to 339818 when printed.

If your var1 was smaller, e.g. 359270 the exact result value (89817.5) would fit into 6 digits and you would see the exact value with the fraction (see demo).

As you already noticed, you can change the default precision.

August 16, 2025 Score: 3 Rep: 20,152 Quality: Medium Completeness: 50%

Is this a floating-point precision issue, or is it related to how cout formats numbers?

In short: the latter.

You can check this easily by using an intermediate variable:

int var1 = 1359270;
int var2 = 4;

double result = static_cast(var1) / var2;

cout