Sum of float and integer and its output

Hello :),

float f = 0.0111;
int i = 1010;

float f2 = i + f;

cout << f2; // output: 1010.01

I expected to get 1010.0111 for the output, but why is this?

How could I get fixed value of 1010.0111 for f2?

Thank you
Welcome!

It's because by default cout has a precision of 6 digits (think of it as 6 sig-figs). [At least on my system, I'm not sure if that is dictated by the standard]

One possible way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    using std::cout;
    
    float f = 0.0111;
    int i = 1010;
    
    float f2 = i + f;
    
    cout.precision(8); // 8 digits (including the left-hand side of decimal)
    cout << f2;
}


[Not to confuse you too much, but note that floating-point numbers are inherently inexact most of the time.
0.0111 cannot be stored exactly as a floating-point number.]
Last edited on
Thank you for the replay and information, Ganado!

I understand that cost has a precision of 6 digits by default and floating-point numbers cannot be stored in float data type as exactly I want to.

Then, how is it possible to get exact floating-point number into a variable?

regards
Last edited on
how is it possible to get exact floating-point number into a variable?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
   double f { 0.0111 };
   int i    { 1010 };

   double f2 { i + f };

   std::cout.precision(10);
   std::cout << f2 << '\n';
}

1010.0111
There is no exact when it comes to floating point.
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Taking Furry Guy's code and expanding precision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
   double f { 0.0111 };
   int i    { 1010 };

   double f2 { i + f };

   std::cout.precision(20);
   std::cout << f2 << '\n';
}

$ g++ -std=c++11 foo.cpp
$ ./a.out 
1010.0111000000000558

That little bit of error at the bottom will creep up on you in many ways.


If you need precise and all significant digits, for example for money, then you use (large enough) integer type and format the output to "human units":
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>

int main()
{
   int i    { 10100111 }; // in cents

   std::cout << '$' << i/100 << '.' << std::setw(2) << std::setfill('0') << i%100 << '\n';
}


If you have a value like 2/3, then you have to carry the {2,3} pair through calculations and again convert to 0.7 only for output.
@hexfffff, would you like to tell us the background to your problem?

Is there any special significance to the fact that your example contains only 0's and 1's?

If you want to maintain such exactitude of decimal representations then you would have to work entirely with strings, and code up some fancy arithmetic operations of your own. It would probably make a nice exercise with classes ...
to a point you can store it as 2 integers, the fraction and the whole parts, where it is understood that the fractional part is divided by some constant (probably a power of 10 for beginners, eg 1 million).

smaller values, and this is relative, can be put in a single integer via the same idea where every number is understood to be divided by some constant, eg pi might be 3141592654 as a 64 bit int, or as 2 ints (3 and 141592654) ... see?

once the 2 integer approach runs dry, you can cook up a big-double class thingy.
Last edited on
Topic archived. No new replies allowed.