当我尝试减去最高值时,我的c ++代码返回扩展表示法。
vector<std::string> smarkdat
vector<std::string> markdat;
vector<double> doubleMdat;
vector<double> sdoubleMdat;
markdat = getmarketbuyData();
smarkdat = getmarketsellData();
transform(smarkdat.begin(), smarkdat.end(), back_inserter(sdoubleMdat), [](string const& val) {return stod(val);});
auto sbiggestMark = std::max_element(std::begin(sdoubleMdat), std::end(sdoubleMdat));
transform(markdat.begin(), markdat.end(), back_inserter(doubleMdat), [](string const& val) {return stod(val);});
auto biggestMark = std::max_element(std::begin(doubleMdat), std::end(doubleMdat));
std::cout << "The highest seller is " << *sbiggestMark << " at position " << std::distance(std::begin(sdoubleMdat), sbiggestMark) << std::endl;
std::vector<double> difference(sdoubleMdat.size(), 0.0);
for ( std::vector<double>::size_type i = 0; i < sdoubleMdat.size(); i++ )
{
difference[i] = std::abs( sbiggestMark[i] - biggestMark[i] );
cout << difference[i] << endl;
}
输出:
8.636e-05
矢量数据是字符串0.000000200
转换为double。
如何返回2个双倍的常规减去值而不是非常明显的错误返回值?
答案 0 :(得分:1)
如果问题是您想要在没有科学记数法的情况下设置输出,请使用std::setprecision
中的std::fixed
和<iomanip>
:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double x = fabs(sqrt(2.0) - sqrt(2.0001));
cout << x << "\n";
cout << setprecision(10) << fixed << x << '\n';
}