0

Union precision values

Why union float values are not precise? Is it just a bug here? https://code.sololearn.com/c87OG0c7PQyG/?ref=app

19th Feb 2019, 9:46 AM
Sav Utu
3 ответов
+ 2
You can't store all possible numbers in a float, because you would need infinite storage. So there is a gap between a float and the next bigger float. If your calculations end up somewhere in between, the result will be rounded. Floating-point maths is never exact!
19th Feb 2019, 10:05 AM
Schindlabua
Schindlabua - avatar
+ 2
That's true, however the rounding happens straight away. When you write down a number literal, you can make that any precision you want. C will happily take float x = 0.120917581976109238109121; But that is way more precision than what fits into a single-precision float. The number will have to be truncated so it fits inside a float. All that happens before the printing, by the way :)
19th Feb 2019, 10:24 AM
Schindlabua
Schindlabua - avatar
+ 1
But I was not calculating in that float yet. It was just saved into memory and then called out to print...
19th Feb 2019, 10:13 AM
Sav Utu