#C 语言标准库宏 compoundn
下面以函数形式说明宏的调用接口。
#include <tgmath.h>
generic_result compoundn(generic_value x, long long int n);
说明
计算 1 加上输入后再取整数次幂,适用于复利或乘法增长因子。 示例的实参类型选择 compoundnf。
x 小于 -1 时发生定义域错误。x 等于 -1 且 n 为负时可能发生极点错误。
适用的二进制实浮点类型和整数参与分派。含 long double 时选长双精度版本,否则含 double 或整数时选双精度版本,其余选择 float 版本。指数、指针、舍入控制或方向参数使用函数原型规定的类型,不参与分派。 实现支持十进制重载时,不能把其参与分派的十进制实参和二进制浮点或复数实参混用。
自 C23 起提供。
参数
x- 每期的相对变化量。n- 整数期数。
返回值
- 返回 (1 + x) 的 n 次幂。
#示例
使用 GCC 时,在链接命令末尾添加 -lm。
#include <tgmath.h>
#include <stdio.h>
int main(void)
{
float computed = compoundn(0.5f, 2);
printf("%Lg\n", (long double)computed);
return !(computed == 2.25);
}
运行结果。
2.25
#相关接口
- 类型通用分派 compoundn、compoundnl、compoundnd32、compoundnd64、compoundnd128
- 相关操作 pow(类型通用宏)、pown(类型通用宏)、rootn(类型通用宏)