I am a beginner to C++ and while studying the difference between double and float, I came across https://www.thoughtco.com/definition-of-double-958065
Now, according to this website, "A double type can represent fractional as well as whole values. It can contain up to 15 digits in total, including those before and after the decimal point. "
So if I want to store a value ,say , 33.53287 I should be able to do that. But when I do so and print my variable, it gives me output 33.5329 . Why is it so? According to the above mentioned line, as long as we are including less than 15 digits, we should be fine. But why such a result then?
Also, I read that double is more precise than float. But If I replace double by float in my code, I get the same results. So how can I say that double is more precise than float?
Not only this, if I replace my number with a bigger 14 digit number (including digits both before and after decimal point), I get an answer of 3.88889e+008 However, if I replace double by int, I get 388888883. Clearly the result obtained by using int as datatype is more precise than the result obtained using double as datatype. Again this is counter-intuitive as int occupies 4 bytes while double 8.
I've seen numerous texts that tell about datatype double but none of them could clear my confusion.
-Understanding the datatype Double
- What is the difference between float and double?
- https://www.geeksforgeeks.org/difference-float-double-c-cpp/
C++ different output in double and float
#include <iostream> using namespace std; int main () { double x=33.53287; cout << x ; return 0; } #include <iostream> using namespace std; int main () { double x=388888883.53287; cout << x ; return 0; }
So basically the question is
why does double not give 33.53287 as output but instead gives 33.5329 despite being highly precise.
even if float gives the same output, why is it less precise than double?
why doesn't double print the entire number in second code- why is int more precise than double in that case.