Is there any advantage to using pow(x,2) instead of x*x, with x double?

前端 未结 8 880
清酒与你
清酒与你 2020-12-03 04:18

is there any advantage to using this code

double x;
double square = pow(x,2);

instead of this?

double x;
double square = x*         


        
8条回答
  •  醉梦人生
    2020-12-03 05:05

    x * x will always compile to a simple multiplication. pow(x, 2) is likely to, but by no means guaranteed, to be optimised to the same. If it's not optimised, it's likely using a slow general raise-to-power math routine. So if performance is your concern, you should always favour x * x.

提交回复
热议问题