나는 총계를 float보다는 double로 모으고 싶습니다.
vector<float> v;
....
double total = std::accumulate(v.begin(),
v.end(),
static_cast<double>(0.0));
//This compiles but I am not sure it is doing what I want
...
double total_ = std::accumulate<double>(v.begin(),
v.end(),
static_cast<double>(0.0)
);
// This doesnt compile, which leads me to believe the first accumulate
// is coercing the double(0.0) back to float.
STL 관용적이고 간결한 작성 방법이 있습니까?
double total = 0.0;
for (int i = 0; i < v.size(); ++i) {
total += v[i];
}
표준 라이브러리 만 사용합니다.
나는 이것이 정밀도를 유지하는 데 충분하지 않다는 것을 알고 있습니다.
에서 사용 된 누적 유형 std::accumulate
은 세 번째 인수의 유형에서 추론됩니다. 리터럴 0.0
은 a double
이므로 double
명시 적 캐스트없이를 사용하여 누적을 수행합니다 .
double total = std::accumulate(v.begin(), v.end(), 0.0);
다음과 같습니다.
double sum = 0.0;
for (f : v) sum += f;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다