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;

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;

Jamesstewart01
Posts: 1
Joined: Thu Jan 25, 2018 1:21 am
Contact:

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

#4 Postby Jamesstewart01 » Thu Jan 25, 2018 1:24 am

thanks for the post

zoraya
Posts: 2
Joined: Tue Apr 26, 2016 5:11 am
Contact:

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

#5 Postby zoraya » Sun Aug 04, 2019 3:41 am

Thanks for the information!

4waytechnologies
Posts: 1
Joined: Tue Mar 31, 2020 6:32 am
Location: California
Contact:

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

#6 Postby 4waytechnologies » Tue Mar 31, 2020 6:34 am

Thanks for the information!

GragMilligan
Posts: 1
Joined: Thu May 06, 2021 12:50 am
Location: https://www.a-writer.com/professional-c ... y-writers/
Contact:

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

#7 Postby GragMilligan » Fri Jul 23, 2021 1:55 am

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

Its works really. Accepting you really wanted to research articles forming and other making works out, we recommend that you visit some great site, actually look at this. I'm an understudy, yet I oftentimes ask him for help since it saves time and gives me results.

catcoin11
Posts: 1
Joined: Sun Mar 20, 2022 2:37 am
Contact:

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

#8 Postby catcoin11 » Sun Mar 20, 2022 3:05 am

Thanks for helping us to your valuable information!
Investing has the best results when you learn from nature.

AlenVeritaz
Posts: 2
Joined: Fri Feb 24, 2023 7:27 am

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

#9 Postby AlenVeritaz » Tue Feb 28, 2023 3:39 am

Hello! thank you for sharing this info!!


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 27 guests