scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl
来自cppreference.com
在标头 <math.h> 定义 | ||
float scalbnf(float arg, intexp); | (1) | (C99 起) |
double scalbn(double arg, intexp); | (2) | (C99 起) |
longdouble scalbnl(longdouble arg, intexp); | (3) | (C99 起) |
在标头 <tgmath.h> 定义 | ||
#define scalbn( arg, exp ) | (4) | (C99 起) |
在标头 <math.h> 定义 | ||
float scalblnf(float arg, longexp); | (5) | (C99 起) |
double scalbln(double arg, longexp); | (6) | (C99 起) |
longdouble scalblnl(longdouble arg, longexp); | (7) | (C99 起) |
在标头 <tgmath.h> 定义 | ||
#define scalbln( arg, exp ) | (8) | (C99 起) |
4,8) 泛型宏:若 arg 拥有 longdouble 类型,则调用
scalbnl
或 scalblnl
。否则,若 arg 拥有整数类型或 double 类型,则调用 scalbn
或 scalbln
。否则调用 scalbnf
或 scalblnf
。目录 |
[编辑]参数
arg | - | 浮点数 |
exp | - | 整数 |
[编辑]返回值
若不出现错误,则返回 arg 乘 FLT_RADIX 的 exp 次幂(arg×FLT_RADIXexp
)。
若出现上溢所致的值域错误,则返回 ±HUGE_VAL、±HUGE_VALF
或 ±HUGE_VALL
。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
[编辑]错误处理
报告 math_errhandling
中指定的错误。
若实现支持 IEEE 浮点数算术(IEC 60559),则
- 决不引发 FE_INEXACT,除非出现值域错误(结果准确)
- 忽略当前舍入模式,除非出现值域错误
- 若 arg 为 ±0,则返回不修改的参数
- 若 arg 为 ±∞,则返回不修改的参数
- 若 exp 为 0,则返回不修改的 arg
- 若 arg 为 NaN,则返回 NaN
[编辑]注解
二进制系统上(其中 FLT_RADIX 为 2
),scalbn 等价于 ldexp
。
尽管指定 scalbn
和 scalbln
高效进行运算,多数实现上它们的效率仍低于用算术运算符乘或除以二的幂。
提供 scalbln
函数,因为从最小正浮点数放大到最大正有限值的因子可能大于标准保证的 INT_MAX 32767。特别是对于 80 位 long double
,因子是 32828。
[编辑]示例
运行此代码
#include <errno.h>#include <fenv.h>#include <float.h>#include <math.h>#include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void){printf("scalbn(7, -4) = %f\n", scalbn(7, -4));printf("scalbn(1, -1074) = %g (double 的最小非正规正值)\n", scalbn(1, -1074));printf("scalbn(nextafter(1,0), 1024) = %g (double 的最大有限值)\n", scalbn(nextafter(1,0), 1024)); // 特殊值printf("scalbn(-0, 10) = %f\n", scalbn(-0.0, 10));printf("scalbn(-Inf, -1) = %f\n", scalbn(-INFINITY, -1)); // 错误处理errno=0;feclearexcept(FE_ALL_EXCEPT);printf("scalbn(1, 1024) = %f\n", scalbn(1, 1024));if(errno==ERANGE)perror(" errno == ERANGE");if(fetestexcept(FE_OVERFLOW))puts(" FE_OVERFLOW raised");}
可能的输出:
scalbn(7, -4) = 0.437500 scalbn(1, -1074) = 4.94066e-324 (double 的最小非正规正值) scalbn(nextafter(1,0), 1024) = 1.79769e+308 (double 的最大有限值) scalbn(-0, 10) = -0.000000 scalbn(-Inf, -1) = -inf scalbn(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[编辑]引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.6.13 The scalbn functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.3.13 The scalbn functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.6.13 The scalbn functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.3.13 The scalbn functions (第 TBD 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.6.13 The scalbn functions (第 247 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.3.13 The scalbn functions (第 523 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.6.13 The scalbn functions (第 228 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.3.13 The scalbn functions (第 460 页)
[编辑]参阅
(C99)(C99) | 将数拆分成有效数字和 2 的幂次 (函数) |
(C99)(C99) | 将数乘以 2 的幂 (函数) |
scalbn 的 C++ 文档 |