名稱空間
變體
操作

std::remainder, std::remainderf, std::remainderl

來自 cppreference.com
< cpp‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
remainder
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <cmath>
(1)
float       remainder ( float x, float y );

double      remainder ( double x, double y );

long double remainder ( long double x, long double y );
(直至 C++23)
constexpr /*浮點型別*/

            remainder ( /*浮點型別*/ x,

                        /*浮點型別*/ y );
(C++23 起)
float       remainderf( float x, float y );
(2) (C++11 起)
(C++23 起為 constexpr)
long double remainderl( 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>

            remainder ( const V0& v_x, const V1& v_y );
(S) (C++26 起)
額外過載 (自 C++11 起)
定義於標頭檔案 <cmath>
template< class Integer >
double      remainder ( Integer x, Integer y );
(A) (C++23 起為 constexpr)
1-3) 計算浮點除法運算 x / y 的 IEEE 餘數。 庫為所有 cv-unqualified 浮點型別提供了 std::remainder 的過載作為引數的型別。(C++23 起)
S) SIMD 過載對 v_xv_y 執行逐元素的 std::remainder
(參閱 math-common-simd-t 的定義。)
(C++26 起)
A) 為所有整數型別提供了額外的過載,它們被視為 double
(C++11 起)

此函式計算的除法運算 x / y 的 IEEE 浮點餘數正好是值 x - quo * y,其中值 quo 是最接近精確值 x / y 的整數值。當 |quo - x / y| = ½ 時,值 quo 被選擇為偶數。

std::fmod 不同,返回值的符號不保證與 x 相同。

如果返回值為零,則其符號與 x 相同。

目錄

[編輯] 引數

x, y - 浮點數或整數值

[編輯] 返回值

如果成功,返回如上定義的除法 x / y 的 IEEE 浮點餘數。

如果發生域錯誤,則返回實現定義的值 (支援 NaN 時返回 NaN)。

如果由於下溢導致範圍錯誤,則返回正確結果。

如果 y 為零,但未發生域錯誤,則返回零。

[編輯] 錯誤處理

錯誤按 math_errhandling 指定的方式報告。

如果 y 為零,則可能發生域錯誤。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • 當前 舍入模式 無效。
  • FE_INEXACT 從不引發,結果始終精確。
  • 如果 x 為 ±∞ 且 y 不是 NaN,則返回 NaN 並引發 FE_INVALID
  • 如果 y 為 ±0 且 x 不是 NaN,則返回 NaN 並引發 FE_INVALID
  • 如果任一引數為 NaN,則返回 NaN。

[編輯] 注意

POSIX 要求,如果 x 為無窮大或 y 為零,則發生域錯誤。

std::fmod,但不是 std::remainder,對於將浮點型別靜默封裝到無符號整數型別很有用:(0.0 <= (y = std::fmod(std::rint(x), 65536.0))) ? y : 65536.0 + y 在範圍 [-0.065535.0] 內,這對應於 unsigned short,但 std::remainder(std::rint(x), 65536.0) 在範圍 [-32767.0+32768.0] 內,超出了 signed short 的範圍。

附加的過載不要求完全按 (A) 提供。它們只需足以確保對於它們的第一個引數 num1 和第二個引數 num2

  • 如果 num1num2 的型別為 long double,則 std::remainder(num1, num2) 的效果與 std::remainder(static_cast<long double>(num1),
                   static_cast<long double>(num2))
    相同。
  • 否則,如果 num1 和/或 num2 的型別為 double 或整數型別,則 std::remainder(num1, num2) 的效果與 std::remainder(static_cast<double>(num1),
                   static_cast<double>(num2))
    相同。
  • 否則,如果 num1num2 的型別為 float,則 std::remainder(num1, num2) 的效果與 std::remainder(static_cast<float>(num1),
                   static_cast<float>(num2))
    相同。
(直至 C++23)

如果 num1num2 具有算術型別,則 std::remainder(num1, num2) 的效果與 std::remainder(static_cast</*common-floating-point-type*/>(num1),
               static_cast</*common-floating-point-type*/>(num2))
相同,其中 /*common-floating-point-type*/ 是具有最高 浮點轉換等級 和最高 浮點轉換子等級 的浮點型別,介於 num1num2 的型別之間,整數型別的引數被認為與 double 具有相同的浮點轉換等級。

如果不存在具有最高等級和次等級的浮點型別,則過載決議不會從提供的過載中產生可用的候選函式。

(C++23 起)

[編輯] 示例

#include <cfenv>
#include <cmath>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    std::cout << "remainder(+5.1, +3.0) = " << std::remainder(5.1, 3) << '\n'
              << "remainder(-5.1, +3.0) = " << std::remainder(-5.1, 3) << '\n'
              << "remainder(+5.1, -3.0) = " << std::remainder(5.1, -3) << '\n'
              << "remainder(-5.1, -3.0) = " << std::remainder(-5.1, -3) << '\n';
 
    // special values
    std::cout << "remainder(-0.0, 1.0) = " << std::remainder(-0.0, 1) << '\n'
              << "remainder(5.1, Inf) = " << std::remainder(5.1, INFINITY) << '\n';
 
    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "remainder(+5.1, 0) = " << std::remainder(5.1, 0) << '\n';
    if (fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

可能的輸出

remainder(+5.1, +3.0) = -0.9
remainder(-5.1, +3.0) = 0.9
remainder(+5.1, -3.0) = -0.9
remainder(-5.1, -3.0) = 0.9
remainder(-0.0, 1.0) = -0
remainder(5.1, Inf) = 5.1
remainder(+5.1, 0) = -nan
    FE_INVALID raised

[編輯] 參閱

計算整數除法的商和餘數
(函式) [編輯]
(C++11)(C++11)
浮點除法運算的餘數
(函式) [編輯]
(C++11)(C++11)(C++11)
帶符號餘數以及除法運算的最後三位
(函式) [編輯]
C 文件remainder