Namespaces
Variants

std:: fdim, std:: fdimf, std:: fdiml

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float fdim ( float x, float y ) ;

double fdim ( double x, double y ) ;

long double fdim ( long double x, long double y ) ;
(C++23 前)
constexpr /*floating-point-type*/

fdim ( /*floating-point-type*/ x,

/*floating-point-type*/ y ) ;
(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 >

fdim ( const V0 & v_x, const V1 & v_y ) ;
(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 限定的浮点类型提供了 std::fdim 的重载版本作为参数类型。 (C++23 起)
S) SIMD 重载对 v_x v_y 执行逐元素的 std::fdim 运算。
(其定义参见 math-common-simd-t
(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 满足以下条件:

  • num1 num2 具有类型 long double ,则 std :: fdim ( num1, num2 ) 的效果等同于 std :: fdim ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) )
  • 否则,若 num1 和/或 num2 具有类型 double 或整数类型,则 std :: fdim ( num1, num2 ) 的效果等同于 std :: fdim ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) )
  • 否则,若 num1 num2 具有类型 float ,则 std :: fdim ( num1, num2 ) 的效果等同于 std :: fdim ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
(C++23 前)

num1 num2 具有算术类型,则 std :: fdim ( num1, num2 ) 的效果等同于 std :: fdim ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
static_cast < /*common-floating-point-type*/ > ( num2 ) )
,其中 /*common-floating-point-type*/ num1 num2 类型之间具有最高 浮点转换等级 和最高 浮点转换子等级 的浮点类型,整数类型的实参被认为具有与 double 相同的浮点转换等级。

若不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用候选。

(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

参见

计算整型数值的绝对值( |x|
(函数)
(C++11) (C++11) (C++11)
两个浮点值的较大者
(函数)
C 文档 关于 fdim