Efficient way to compute p^q (exponentiation), where q is an integer

前端 未结 3 1518
时光取名叫无心
时光取名叫无心 2020-12-15 20:33

What is an efficient way to compute pq, where q is an integer?

3条回答
  •  青春惊慌失措
    2020-12-15 21:04

    Assuming that ^ means exponentiation and that q is runtime variable, use std::pow(double, int).

    EDIT: For completeness due to the comments on this answer: I asked the question Why was std::pow(double, int) removed from C++11? about the missing function and in fact pow(double, int) wasn't removed in C++0x, just the language was changed. However, it appears that libraries may not actually optimize it due to result accuracy concerns.

    Even given that I would still use pow until measurement showed me that it needed to be optimized.

提交回复
热议问题