std::fdim, std::fdimf, std::fdiml
来自 cppreference.cn
定义于头文件 <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 起) (constexpr 自 C++23 起) |
long double fdiml( long double x, long double y ); |
(3) | (自 C++11 起) (constexpr 自 C++23 起) |
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) | (constexpr 自 C++23 起) |
1-3) 返回 x 和 y 之间的正差,即,如果 x > y,则返回 x - y,否则(即如果 x <= y)返回 +0。 库为所有 cv 限定的浮点类型作为参数类型提供了
std::fdim
的重载。(自 C++23 起)
S) SIMD 重载对 v_x 和 v_y 执行元素级
std::fdim 。
|
(自 C++26 起) |
A) 为所有整数类型提供了附加重载,这些重载被视为 double。
|
(自 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'; // error handling 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: Numerical result out of range FE_OVERFLOW raised
[编辑] 参见
(C++11) |
计算整数值的绝对值 (|x|) (函数) |
(C++11)(C++11)(C++11) |
两个浮点值中的较大值 (函数) |
C 文档 对于 fdim
|