How do you do exponentiation in C?

前端 未结 7 1513
执笔经年
执笔经年 2020-12-05 06:31

I tried \"x = y ** e\", but that didn\'t work.

7条回答
  •  忘掉有多难
    2020-12-05 06:49

    use the pow function (it takes floats/doubles though).

    man pow:

       #include 
    
       double pow(double x, double y);
       float powf(float x, float y);
       long double powl(long double x, long double y);
    

    EDIT: For the special case of positive integer powers of 2, you can use bit shifting: (1 << x) will equal 2 to the power x. There are some potential gotchas with this, but generally, it would be correct.

提交回复
热议问题