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

前端 未结 8 904
清酒与你
清酒与你 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 04:55

    IMHO:

    • Code readability
    • Code robustness - will be easier to change to pow(x, 6), maybe some floating point mechanism for a specific processor is implemented, etc.
    • Performance - if there is a smarter and faster way to calculate this (using assembler or some kind of special trick), pow will do it. you won't.. :)

    Cheers

提交回复
热议问题