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 /*浮点类型*/ fdim ( /*浮点类型*/ 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) |
1-3) 返回 x 和 y 之间的正差值,即如果 x > y,则返回 x - y,否则(即如果 x <= y)返回 +0。 库为所有 cv-unqualified 浮点类型提供了
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 文档 for fdim
| |