std:: fdim, std:: fdimf, std:: fdiml
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
fdim
(
float
x,
float
y
)
;
double
fdim
(
double
x,
double
y
)
;
|
(C++23 前) | |
|
constexpr
/*floating-point-type*/
fdim
(
/*floating-point-type*/
x,
|
(C++23 起) | |
|
float
fdimf
(
float
x,
float
y
)
;
|
(2) |
(C++11 起)
(C++23 起为 constexpr) |
|
long
double
fdiml
(
long
double
x,
long
double
y
)
;
|
(3) |
(C++11 起)
(C++23 起为 constexpr) |
|
SIMD 重载
(C++26 起)
|
||
|
定义于头文件
<simd>
|
||
|
template
<
class
V0,
class
V1
>
constexpr
/*math-common-simd-t*/
<
V0, V1
>
|
(S) | (C++26 起) |
|
附加重载
(C++11 起)
|
||
|
定义于头文件
<cmath>
|
||
|
template
<
class
Integer
>
double fdim ( Integer x, Integer y ) ; |
(A) | (C++23 起为 constexpr) |
std::fdim
的重载版本作为参数类型。
(C++23 起)
|
S)
SIMD 重载对
v_x
和
v_y
执行逐元素的
std::fdim
运算。
|
(since C++26) |
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since C++11) |
目录 |
参数
| x, y | - | 浮点数或整数值 |
返回值
若成功,返回 x 与 y 之间的正差值。
如果发生因溢出导致的范围错误,将返回
+HUGE_VAL
、
+HUGE_VALF
或
+HUGE_VALL
。
如果发生因下溢导致的范围错误,将返回正确值(四舍五入后)。
错误处理
错误报告方式遵循 math_errhandling 中的规范。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 如果任一参数为 NaN,则返回 NaN。
注释
等价于 std:: fmax ( x - y, 0 ) ,但 NaN 处理要求除外。
额外的重载并不需要完全按照 (A) 的形式提供。只需确保对于它们的第一个参数 num1 和第二个参数 num2 满足以下条件:
|
(C++23 前) |
|
若
num1
和
num2
具有算术类型,则
std
::
fdim
(
num1, num2
)
的效果等同于
std
::
fdim
(
static_cast
<
/*common-floating-point-type*/
>
(
num1
)
,
若不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用候选。 |
(C++23 起) |
示例
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> #ifndef __GNUC__ #pragma STDC FENV_ACCESS ON #endif int main() { std::cout << "fdim(4, 1) = " << std::fdim(4, 1) << '\n' << "fdim(1, 4) = " << std::fdim(1, 4) << '\n' << "fdim(4,-1) = " << std::fdim(4, -1) << '\n' << "fdim(1,-4) = " << std::fdim(1, -4) << '\n'; // 错误处理 errno = 0; std::feclearexcept(FE_ALL_EXCEPT); std::cout << "fdim(1e308, -1e308) = " << std::fdim(1e308, -1e308) << '\n'; if (errno == ERANGE) std::cout << " errno == ERANGE: " << std::strerror(errno) << '\n'; if (std::fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW raised\n"; }
输出:
fdim(4, 1) = 3
fdim(1, 4) = 0
fdim(4,-1) = 5
fdim(1,-4) = 5
fdim(1e308, -1e308) = inf
errno == ERANGE: 数值结果超出范围
FE_OVERFLOW raised
参见
|
(C++11)
|
计算整型数值的绝对值(
|x|
)
(函数) |
|
(C++11)
(C++11)
(C++11)
|
两个浮点值的较大者
(函数) |
|
C 文档
关于
fdim
|
|