I'm using C to read a file and assigning values from the file to parameters but the float numbers are no shown correctly when I cout the parameter value . Is the problem with the cout or with the actual value assigned to parameter? For instance, 21599.760000 is shown as 21599.8
CodePudding user response:
It's hard to be precise without some code example. For future questions it's best to share a minimal code example.
Reading from
21599.760000 is shown as 21599.8
I assume it's the default precision being defined as 1.
The output precision for string conversion via std::cout
can be defined with std::setprecision.
For example std::setprecision(3);
will let look 1.54321 like 1.543.
CodePudding user response:
The default precision of output streams is 6 so if you haven't changed the precision then you are printing the value with 6 significant digits. You can change the precision using std::setprecision
.
Note that the actual value stored in float
(probably) cannot be 21599.760000 because that value is not representable as a single precision IEEE-754 floating point number. The closest representable value is 21599.759765625. In this particular case, you can get ouput with same value as the input by using precision in range [7, 16].