Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      pow, powf, powl

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      pow
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      (C99)(C23)
      (C99)
      Narrowing operations
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      Quantum and quantum exponent
      Decimal re-encoding functions
      Total order and payload functions
      Classification
      Error and gamma functions
      (C99)
      (C99)
      (C99)
      (C99)
      Types
      Macro constants
      Special floating-point values
      (C99)(C23)
      Arguments and return values
      Error handling
      Fast operation indicators
       
      Defined in header<math.h>
      float powf(float base,float exponent);
      (1)(since C99)
      double pow(double base,double exponent);
      (2)
      longdouble powl(longdouble base,longdouble exponent);
      (3)(since C99)
      Defined in header<tgmath.h>
      #define pow( base, exponent )
      (4)(since C99)
      1-3) Computes the value ofbase raised to the powerexponent.
      4) Type-generic macro: If any argument has typelongdouble,powl is called. Otherwise, if any argument has integer type or has typedouble,pow is called. Otherwise,powf is called. If at least one argument is complex or imaginary, then the macro invokes the corresponding complex function (cpowf,cpow,cpowl).

      Contents

      [edit]Parameters

      base - base as floating-point value
      exponent - exponent as floating-point value

      [edit]Return value

      If no errors occur,base raised to the power ofexponent (baseexponent
      ) is returned.

      If a domain error occurs, an implementation-defined value is returned (NaN where supported).

      If a pole error or a range error due to overflow occurs,±HUGE_VAL,±HUGE_VALF, or±HUGE_VALL is returned.

      If a range error occurs due to underflow, the correct result (after rounding) is returned.

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      Ifbase is finite and negative andexponent is finite and non-integer, a domain error occurs and a range error may occur.

      Ifbase is zero andexponent is zero, a domain error may occur.

      Ifbase is zero andexponent is negative, a domain error or a pole error may occur.

      If the implementation supports IEEE floating-point arithmetic (IEC 60559),

      • pow(+0, exponent), whereexponent is a negative odd integer, returns+∞ and raisesFE_DIVBYZERO
      • pow(-0, exponent), whereexponent is a negative odd integer, returns-∞ and raisesFE_DIVBYZERO
      • pow(±0, exponent), whereexponent is negative, finite, and is an even integer or a non-integer, returns +∞ and raisesFE_DIVBYZERO
      • pow(±0,-) returns +∞and may raiseFE_DIVBYZERO(until C23)
      • pow(+0, exponent), whereexponent is a positive odd integer, returns +0
      • pow(-0, exponent), whereexponent is a positive odd integer, returns -0
      • pow(±0, exponent), whereexponent is positive non-integer or a positive even integer, returns +0
      • pow(-1, ±∞) returns1
      • pow(+1, exponent) returns1 for anyexponent, even whenexponent isNaN
      • pow(base, ±0) returns1 for anybase, even whenbase isNaN
      • pow(base, exponent) returnsNaN and raisesFE_INVALID ifbase is finite and negative andexponent is finite and non-integer.
      • pow(base,-) returns +∞ for any|base|<1
      • pow(base,-) returns +0 for any|base|>1
      • pow(base,+) returns +0 for any|base|<1
      • pow(base,+) returns +∞ for any|base|>1
      • pow(-∞, exponent) returns -0 ifexponent is a negative odd integer
      • pow(-∞, exponent) returns +0 ifexponent is a negative non-integer or negative even integer
      • pow(-∞, exponent) returns -∞ ifexponent is a positive odd integer
      • pow(-∞, exponent) returns +∞ ifexponent is a positive non-integer or positive even integer
      • pow(+∞, exponent) returns +0 for any negativeexponent
      • pow(+∞, exponent) returns +∞ for any positiveexponent
      • except where specified above, if any argument is NaN, NaN is returned.

      [edit]Notes

      Althoughpow cannot be used to obtain a root of a negative number,cbrt is provided for the common case whereexponent is1/3.

      [edit]Example

      Run this code
      #include <errno.h>#include <fenv.h>#include <math.h>#include <stdio.h>// #pragma STDC FENV_ACCESS ON int main(void){// typical usageprintf("pow(2, 10) = %f\n", pow(2,10));printf("pow(2, 0.5) = %f\n", pow(2,0.5));printf("pow(-2, -3) = %f\n", pow(-2,-3)); // special valuesprintf("pow(-1, NAN) = %f\n", pow(-1, NAN));printf("pow(+1, NAN) = %f\n", pow(+1, NAN));printf("pow(INFINITY, 2) = %f\n", pow(INFINITY,2));printf("pow(INFINITY, -1) = %f\n", pow(INFINITY,-1)); // error handlingerrno=0;feclearexcept(FE_ALL_EXCEPT);printf("pow(-1, 1/3) = %f\n", pow(-1,1.0/3));if(errno==EDOM)perror("    errno == EDOM");if(fetestexcept(FE_INVALID))puts("    FE_INVALID raised"); feclearexcept(FE_ALL_EXCEPT);printf("pow(-0, -3) = %f\n", pow(-0.0,-3));if(fetestexcept(FE_DIVBYZERO))puts("    FE_DIVBYZERO raised");}

      Possible output:

      pow(2, 10) = 1024.000000pow(2, 0.5) = 1.414214pow(-2, -3) = -0.125000pow(-1, NAN) = nanpow(+1, NAN) = 1.000000pow(INFINITY, 2) = infpow(INFINITY, -1) = 0.000000pow(-1, 1/3) = -nan    errno == EDOM: Numerical argument out of domain    FE_INVALID raisedpow(-0, -3) = -inf    FE_DIVBYZERO raised

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.7.5 The pow functions
      • 7.27 Type-generic math <tgmath.h>
      • F.10.4.5 The pow functions (p: 524-525)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.7.4 The pow functions (p: 248-249)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.4.4 The pow functions (p: 524-525)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.7.4 The pow functions (p: 248-249)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.4.4 The pow functions (p: 524-525)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.7.4 The pow functions (p: 229)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.4.4 The pow functions (p: 461)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.5.5.1 The pow function

      [edit]See also

      (C99)(C99)
      computes square root (\(\small{\sqrt{x} }\)x)
      (function)[edit]
      (C99)(C99)(C99)
      computes cube root (\(\small{\sqrt[3]{x} }\)3x)
      (function)[edit]
      (C99)(C99)(C99)
      computes square root of the sum of the squares of two given numbers (\(\scriptsize{\sqrt{x^2+y^2} }\)x2
      +y2
      )
      (function)[edit]
      (C99)(C99)(C99)
      computes the complex power function
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/pow&oldid=172055"

      [8]ページ先頭

      ©2009-2025 Movatter.jp