What if integral meet float point? type conversions.

Books don't say more about when integer meet float.

1
2
3
4
5
cout.setf(ios_base::fixed);
float f = 0;
unsigned long long ull = ULLONG_MAX;
f = ull;
cout << f << endl << ull;


the output's precision is 16, but float' s precision is 6. why?
When an integer is implicitly converted into a float, its value is rounded to the nearest valid float, see http://en.cppreference.com/w/cpp/language/implicit_cast#Floating_-_integral_conversions
(or, for C, http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions

Your ull is 18446744073709551615 which lies between the floats 18446744073709551616.0 and 18446744073709549568.0. It is much closer to 18446744073709551616.0, so that's the value you get.

You've chosen "fixed" output format, so precision is irrelevant
Topic archived. No new replies allowed.