0

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

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.

user185887
  • 643
  • 6
  • 18
  • Your way of _checking_ the values is flawed. [std::setprecision](https://en.cppreference.com/w/cpp/io/manip/setprecision) The difference is in datatype, but you use the same method to print it, with 6 digits precision. – KamilCuk Jul 12 '19 at 12:42
  • Be aware that there are some numbers left not exceeding those 15 digits but still not being representable in double – not even such a simple number as 0.2, which is periodic in binary representation... – Aconcagua Jul 12 '19 at 12:51

1 Answers1

1

You're right about the data type, but you're not considering how cout works. Streams have a number of formatting options, and by default they give you fewer significant figures than you're looking for in this case. They try to be intelligent about it but it's not possible to cover all needs automatically.

To get what you want, try the std::fixed I/O manipulator:

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
    double  x=388888883.53287;
    cout  << std::fixed << x ;
    return 0;
}

// Output: 388888883.532870

(live demo)

More generally, try using your debugger, instead of streams, when you want to forensically examine data in your program. That keeps external factors from affecting your results without your realising it!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055