#need help understanding output after using setprecision.
16 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
in C++ the floating point types can represent a limited amount of significant numbers with precision. the limit is decided by the IEEE-754 floating point representation.
The first output is a number with float type, a float supports a precision up to 6-9 numbers, typically 7.. that means It'll always represent the first 6 numbers accurately, and it may represent up to 9. but It usually represent accurately up to 7
the second output is a number with double type, a double supports a precision of siginifcant digits up to 15-18, typically 16
Although I don't know why we're limited in the first place, perhaps someone who's more knowledgeable than me will explain it
Thank your for your response, isn't the point of setprecision() to adjust the precision of float? Why does it not extend it beyond 6 even after setting it as far as 17?
It's just to set precision as in float converted to text can have as far as N literals after ., not the precision of the float itself
No, you do not adjust the precision of "float", instead you adjust the precision of the output
by default the output cannot be accurate for more than 6 signifcant digits "even if you used double"
number.N_literals_set_by_setprecision
and since float can store only range of numbers, it cannot magically figure out what are next literals, let's say to infinity
it does not calculate further what float would store
ohh ok my bad, i didnt have my concepts clear
how does the language determine what numbers are put in after 6 numbers?
i dont think its randomized as it gives the same output everytime
This stems from the fact how we store floating point numbers/fractionals.
float and double have their own representation, one cannot simply read the number as would int.
What we really store is aproximation of a number, usually a pretty good approximation, that allows us to also store numbers that supposedly would need infinite memory after ., infinite decimal expansion (does not really let us to go to infinity per se)?
We methodically store information required to get the actual number as human usually would write it, if needed.
That also allows us to figure out where the . should be placed.
https://en.wikipedia.org/wiki/Floating-point_arithmetic
Here you have possible maximum values for different primitive types:
https://en.cppreference.com/w/cpp/types/numeric_limits