How is pow() calculated in C?

前端 未结 4 2117
慢半拍i
慢半拍i 2021-01-05 09:42

Our professor said that you can\'t calculate ab if a<0 using pow() because pow() uses natural logarithms to calculate it (ab

4条回答
  •  鱼传尺愫
    2021-01-05 10:21

    pow does work for negative numbers. It just doesn't work when the base is negative and the exponent is not an integer.

    A number in the form ax/y actually involves the y-th root of x. For instance, when you try to calculate a1/2 you are actually looking for the square root of a.

    So what happens if you have a negative base and a non-integer exponent? You get a y-th root of a negative number, which yields is a complex non-real number. pow() does not work with complex numbers, so it will probably return NaN.

提交回复
热议问题