std::fdim, std::fdimf, std::fdiml
來自 cppreference.com
定義於標頭檔案 <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
|