常用数学函数
计算立方根 (3√x)
std::cbrt,
std::cbrtf,
std::cbrtl
float cbrtf( float arg ); | (1) | (C99 起) |
double cbrt( double arg ); | (2) | (C99 起) |
long double cbrtl( long double arg ); | (3) | (C99 起) |
定义于头文件 | ||
#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) 不等价于 pow(arg, 1.0/3) ,因为 pow 不能求负底数的小数次幂。
调用示例
#include <iostream>
#include <cstdlib>
#include <typeinfo>
#include <cinttypes>
#include <cmath>
#include <tgmath.h>
int main()
{
//1-3) 计算 arg 的平方根。
const float fNumber = 0.1314;
std::cout << "typeid(float).name(): " << typeid(float).name() << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << fNumber + i << "): "
<< std::cbrt(fNumber + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << -fNumber - i << "): "
<< std::cbrt(-fNumber - i) << std::endl;
}
std::cout << std::endl;
const double dNumber = 0.01314;
std::cout << "typeid(double).name(): " << typeid(double).name() << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << dNumber + i << "): "
<< std::cbrt(dNumber + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << -dNumber - i << "): "
<< std::cbrt(-dNumber - i) << std::endl;
}
std::cout << std::endl;
const long double ldNumber = 0.001314;
std::cout << "typeid(long double).name(): " << typeid(long double).name() << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << ldNumber + i << "): "
<< std::cbrt(ldNumber + i) << std::endl;
}
std::cout << std::endl;
for (int i = 0; i < 1000; i += 100)
{
std::cout << "std::cbrt(" << -ldNumber - i << "): "
<< std::cbrt(-ldNumber - i) << std::endl;
}
std::cout << std::endl;
return 0;
}
输出
typeid(float).name(): f
std::cbrt(0.1314): 0.508392
std::cbrt(100.131): 4.64362
std::cbrt(200.131): 5.84932
std::cbrt(300.131): 6.69531
std::cbrt(400.131): 7.36887
std::cbrt(500.131): 7.9377
std::cbrt(600.131): 8.43494
std::cbrt(700.131): 8.8796
std::cbrt(800.131): 9.28369
std::cbrt(900.131): 9.65536
std::cbrt(-0.1314): -0.508392
std::cbrt(-100.131): -4.64362
std::cbrt(-200.131): -5.84932
std::cbrt(-300.131): -6.69531
std::cbrt(-400.131): -7.36887
std::cbrt(-500.131): -7.9377
std::cbrt(-600.131): -8.43494
std::cbrt(-700.131): -8.8796
std::cbrt(-800.131): -9.28369
std::cbrt(-900.131): -9.65536
typeid(double).name(): d
std::cbrt(0.01314): 0.235975
std::cbrt(100.013): 4.64179
std::cbrt(200.013): 5.84816
std::cbrt(300.013): 6.69443
std::cbrt(400.013): 7.36814
std::cbrt(500.013): 7.93707
std::cbrt(600.013): 8.43439
std::cbrt(700.013): 8.8791
std::cbrt(800.013): 9.28323
std::cbrt(900.013): 9.65494
std::cbrt(-0.01314): -0.235975
std::cbrt(-100.013): -4.64179
std::cbrt(-200.013): -5.84816
std::cbrt(-300.013): -6.69443
std::cbrt(-400.013): -7.36814
std::cbrt(-500.013): -7.93707
std::cbrt(-600.013): -8.43439
std::cbrt(-700.013): -8.8791
std::cbrt(-800.013): -9.28323
std::cbrt(-900.013): -9.65494
typeid(long double).name(): e
std::cbrt(0.001314): 0.10953
std::cbrt(100.001): 4.64161
std::cbrt(200.001): 5.84805
std::cbrt(300.001): 6.69434
std::cbrt(400.001): 7.36807
std::cbrt(500.001): 7.93701
std::cbrt(600.001): 8.43433
std::cbrt(700.001): 8.87905
std::cbrt(800.001): 9.28318
std::cbrt(900.001): 9.6549
std::cbrt(-0.001314): -0.10953
std::cbrt(-100.001): -4.64161
std::cbrt(-200.001): -5.84805
std::cbrt(-300.001): -6.69434
std::cbrt(-400.001): -7.36807
std::cbrt(-500.001): -7.93701
std::cbrt(-600.001): -8.43433
std::cbrt(-700.001): -8.87905
std::cbrt(-800.001): -9.28318
std::cbrt(-900.001): -9.6549