首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::pow

Defined in header <cmath>

?

?

float pow( float base, float exp );

(1)

?

double pow( double base, double exp );

(2)

?

long double pow( long double base, long double exp );

(3)

?

float pow( float base, int iexp );

(4)

(until C++11)

double pow( double base, int iexp );

(5)

(until C++11)

long double pow( long double base, int iexp );

(6)

(until C++11)

Promoted pow( Arithmetic1 base, Arithmetic2 exp );

(7)

(since C++11)

1-6%29计算base升到权力expiexp...

7%29一组过载或函数模板,用于1-3%29未涵盖的算术类型的所有参数组合。如果有任何争论积分型,它被铸造成double.如果有任何争论long double,则返回类型Promoted也是long double,否则返回类型总是double...

参数

base

-

base as a value of floating-point or integral type

exp

-

exponent as a value of floating-point or integral type

iexp

-

exponent as integer value

返回值

如果没有错误发生,base提升到…的力量exp%28或iexp%29%28 base exp

%29,返回。

如果发生域错误,则返回支持%29的实现定义值%28 NaN。

如果由于溢出而产生极差或距离误差,±HUGE_VAL,,,±HUGE_VALF,或±HUGE_VALL会被归还。

如果由于下流而发生范围错误,则返回舍入%29后的正确结果%28。

错误处理

如果base是有限的和负的exp是有限且非整数的,则可能发生域错误和范围错误.

如果base是零和exp为零,则可能发生域错误。

如果base是零和exp为负值,则可能出现域错误或极错误。

如果实现支持ieee浮点算法%28IEC 60559%29,

  • pow(+0, exp),在哪里exp是一个负奇数,返回+∞并引发FE_DIVBYZERO
  • pow(-0, exp),在哪里exp是一个负奇数,返回-∞并引发FE_DIVBYZERO
  • pow(±0, exp),在哪里exp是负的,有限的,并且是偶数或非整数,返回+∞并引发FE_DIVBYZERO
  • pow(±0, -∞)返回+∞并可能引发FE_DIVBYZERO
  • pow(+0, exp),在哪里exp为正奇数,则返回+0。
  • pow(-0, exp),在哪里exp是一个正数整数,返回-0。
  • pow(±0, exp),在哪里exp为正非整数或正偶数,则返回+0。
  • pow(-1, ±∞)回报1
  • pow(+1, exp)回报1对任何exp,即使当expNaN
  • pow(base, ±0)回报1对任何base,即使当baseNaN
  • pow(base, exp)回报NaN并提高FE_INVALID如果base是有限的和负的exp是有限的和非整数的。
  • pow(base, -∞)返回任意值的+∞|base|<1
  • pow(base, -∞)任何返回+0|base|>1
  • pow(base, +∞)任何返回+0|base|<1
  • pow(base, +∞)返回任意值的+∞|base|>1
  • pow(-∞, exp)返回-0如果exp是一个负奇数。
  • pow(-∞, exp)返回+0如果exp是负的非整数或偶数。
  • pow(-∞, exp)返回-∞假设exp是一个正奇数。
  • pow(-∞, exp)返回+∞如果exp是正数或偶数。
  • pow(+∞, exp)任何负数返回+0exp
  • pow(+∞, exp)对任何正数返回+∞exp
  • 除上述指定外,如果任何参数为nan,则返回nan。

注记

pow(float, int)回报float直到C++11%每过载4%29但返回double自C++11%每过载28次7%29。

尽管std::pow不能用于获取负数的根,std::cbrt为常见情况提供exp是1/3。

二次

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <cerrno>
#include <cfenv>
#include <cstring>
 
#pragma STDC FENV_ACCESS ON
int main()
{
    // typical usage
    std::cout << "pow(2, 10) = " << std::pow(2,10) << '\n'
              << "pow(2, 0.5) = " << std::pow(2,0.5) << '\n'
              << "pow(-2, -3) = " << std::pow(-2,-3) << '\n';
    // special values
    std::cout << "pow(-1, NAN) = " << std::pow(-1,NAN) << '\n'
              << "pow(+1, NAN) = " << std::pow(+1,NAN) << '\n'
              << "pow(INFINITY, 2) = " << std::pow(INFINITY, 2) << '\n'
              << "pow(INFINITY, -1) = " << std::pow(INFINITY, -1) << '\n';
    // error handling 
    errno = 0; std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "pow(-1, 1/3) = " << std::pow(-1, 1.0/3) << '\n';
    if(errno == EDOM) std::cout << "    errno == EDOM " << std::strerror(errno) << '\n';
    if(std::fetestexcept(FE_INVALID)) std::cout << "    FE_INVALID raised\n";
 
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "pow(-0, -3) = " << std::pow(-0.0, -3) << '\n';
    if(std::fetestexcept(FE_DIVBYZERO)) std::cout << "    FE_DIVBYZERO raised\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
pow(2, 10) = 1024
pow(2, 0.5) = 1.41421
pow(-2, -3) = -0.125
pow(-1, NAN) = nan
pow(+1, NAN) = 1
pow(INFINITY, 2) = inf
pow(INFINITY, -1) = 0
pow(-1, 1/3) = -nan
    errno == EDOM Numerical argument out of domain
    FE_INVALID raised
pow(-0, -3) = -inf
    FE_DIVBYZERO raised

二次

另见

sqrt

computes square root (√x) (function)

cbrt (C++11)

computes cubic root (3√x) (function)

hypot (C++11)

computes square root of the sum of the squares of two given numbers (√x2+y2) (function)

pow(std::complex)

complex power, one or both arguments may be a complex number (function template)

pow(std::valarray)

applies the function std::pow to two valarrays or a valarray and a value (function template)

c关于POW的文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com