Namespaces
Variants

cbrt, cbrtf, cbrtl

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

Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
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
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float cbrtf ( float arg ) ;
(1) (C99 起)
double cbrt ( double arg ) ;
(2) (C99 起)
long double cbrtl ( long double arg ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define cbrt( arg )
(4) (C99 起)
1-3) 计算 arg 的立方根。
4) 类型泛型宏:若 arg 具有类型 long double ,则调用 cbrtl 。否则,若 arg 具有整数类型或类型 double ,则调用 cbrt 。否则,调用 cbrtf

目录

参数

arg - 浮点数值

返回值

若无错误发生,则返回 arg 的立方根( 3 arg )。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 如果参数为 ±0 或 ±∞,则保持不变返回
  • 如果参数为 NaN,则返回 NaN

注释

cbrt ( arg ) is not equivalent to pow ( arg, 1.0 / 3 ) because the rational number
1
3
is typically not equal to 1.0 / 3 and std::pow cannot raise a negative base to a fractional exponent. Moreover, cbrt ( arg ) usually gives more accurate results than pow ( arg, 1.0 / 3 ) (see example).

示例

#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("常规用法:\n"
           "cbrt(729)      = %f\n", cbrt(729));
    printf("cbrt(-0.125)   = %f\n", cbrt(-0.125));
    printf("特殊值:\n"
           "cbrt(-0)       = %f\n", cbrt(-0.0));
    printf("cbrt(+inf)     = %f\n", cbrt(INFINITY));
    printf("精度:\n"
           "cbrt(343)      = %.*f\n", DBL_DECIMAL_DIG, cbrt(343));
    printf("pow(343,1.0/3) = %.*f\n", DBL_DECIMAL_DIG, pow(343, 1.0/3));
}

可能的输出:

常规用法:
cbrt(729)      = 9.000000
cbrt(-0.125)   = -0.500000
特殊值:
cbrt(-0)       = -0.000000
cbrt(+inf)     = inf
精度:
cbrt(343)      = 7.00000000000000000
pow(343,1.0/3) = 6.99999999999999911

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.7.1 cbrt 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.4.1 cbrt 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.7.1 cbrt 函数 (p: 180-181)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.4.1 cbrt 函数 (p: 381-)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.7.1 cbrt 函数 (p: 247)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.4.1 cbrt 函数 (p: 524)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.7.1 cbrt函数(页码:228)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.4.1 cbrt函数(页码:460)

参见

(C99) (C99)
计算一个数的指定次幂 ( x y )
(函数)
(C99) (C99)
计算平方根 ( x )
(函数)
(C99) (C99) (C99)
计算两个给定数平方和的平方根 ( x 2
+y 2
)
(函数)