2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

accumulateの第三引数に注意(及び数値の型指定)

Last updated at Posted at 2021-04-24

vector<long long>型やvector<double>型の配列 x の総和を求めるときに,

std::accumulate(x.begin(), x.end(), 0);

とすると誤差がおおきくなったり,オーバーフローしたりする。std::accumulateの仕様として第三引数の型で値が帰ってくるらしい。なので正しくはそれぞれ

std::accumulate(x.begin(), x.end(), 0LL);
std::accumulate(x.begin(), x.end(), 0.0L);

とする。一応数値の型指定のまとめしておく:

0          //int
0.0        //float
0LL        //int64_t
0.0L       //double

(メモ) std::cout よりも printf のほうが便利で早いこともあるっぽい,桁数固定など

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?