Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double multiplication giving rounded results

double a = 2451550;
double b = .407864;
double c= a*b;
cout<<c;

I was expecting the results to be "999898.9892" but getting "999899". I need the actual unrounded result.Please suggest.

like image 865
Vikas Avatar asked Dec 10 '22 12:12

Vikas


2 Answers

By default, iostreams output 6 digits of precision. If you want more, you have to ask for it:

std::cout.precision(15);
like image 144
Drew Hall Avatar answered Dec 28 '22 04:12

Drew Hall


It can also be done using Manipulator setprecision like below.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
 double a = 2451550;
 double b = .407864;
 double c= a*b;
 cout<<setprecision(15)<<c;
}

Also, Usage of manipulator will make the code compact.

like image 30
bjskishore123 Avatar answered Dec 28 '22 02:12

bjskishore123