how to set precision for double in C++

Examples step by step how to make your own automated strategy or user indicator
Message
Author
Ktanger
Posts: 1
Joined: Mon Apr 18, 2016 2:15 pm

how to set precision for double in C++

#1 Postby Ktanger » Mon Apr 18, 2016 2:27 pm

Hi
Please check below MQL4 programming first:

double USOilAskStart = MarketInfo("USOIL", MODE_BID);
double UKOilAskStart = MarketInfo("UKOIL", MODE_BID);
double PriceGapStart = UKOilAskStart - USOilAskStart;
if (NormalizeDouble(PriceGapStart, 3) >= 1.500)
{
...
}
the question is:
I want to find solution in C++ to set precision for double data like MQL4 above "NormalizeDouble(PriceGapStart, 3)" so that i can compare the result, any one can help me?

violajsilver
Posts: 5
Joined: Mon May 09, 2016 11:42 pm

Re: how to set precision for double in C++

#2 Postby violajsilver » Mon May 09, 2016 11:49 pm

You can set the precision directly on std::cout and used the std::fixed format specifier.

Code: Select all

double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;

You can #include <limits> to get the maximum precision of a float or double.

Code: Select all

#include <limits>

typedef std::numeric_limits< double > dbl;

double d = 3.14159265358979;
cout.precision(dbl::max_digits10);
cout << "Pi: " << fixed << d << endl;


custom writings

mark1205
Posts: 1
Joined: Wed Aug 31, 2016 4:05 am

Re: how to set precision for double in C++

#3 Postby mark1205 » Wed Aug 31, 2016 4:21 am

std::cout << std::setprecision (std::numeric_limits<double>::digits10 + 1)
<< 3.14159265358979
<< std::endl;


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 1 guest