最終更新日時(UTC):
が更新

履歴 編集

function
<cmath>

std::remquo(C++11)

namespace std {
  float
    remquo(float x,
           float y,
           int* quo);             // (1) C++11からC++20まで
  double
    remquo(double x,
           double y,
           int* quo);             // (2) C++11からC++20まで
  long double
    remquo(long double x,
           long double y,
           int* quo);             // (3) C++11からC++20まで

  constexpr floating-point-type
    remquo(floating-point-type x,
           floating-point-type y,
           int* quo);             // (4) C++23

  Integral
    remquo(Integral x,
           Integral y,
           int* quo);             // (5) C++11
  constexpr Integral
    remquo(Integral x,
           Integral y,
           int* quo);             // (5) C++23

  float
    remquof(float x,
            float y,
            int* quo);            // (6) C++17
  constexpr float
    remquof(float x,
            float y,
            int* quo);            // (6) C++23

  long double
    remquol(long double x,
            long double y,
            int* quo);            // (7) C++17
  constexpr long double
    remquol(long double x,
            long double y,
            int* quo);            // (7) C++23
}

概要

浮動小数点数の剰余と、商の一部を求める。remquoは、「remainder (剰余)」と「quotient (商)」と意味する。

この関数は、浮動小数点数に対して除算を行い、除算の結果(商)の一部と、その余り(剰余)を同時に求める。戻り値として剰余が返され、ポインタ引数quoに商の値の一部が書き込まれる。

  • (1) : floatに対するオーバーロード
  • (2) : doubleに対するオーバーロード
  • (3) : long doubleに対するオーバーロード
  • (4) : 浮動小数点数型に対するオーバーロード
  • (5) : 整数型に対するオーバーロード (doubleにキャストして計算される)
  • (6) : float型規定
  • (7) : long double型規定

戻り値

  • remainder()関数と同じ方法で剰余を求めて、戻り値として返す。
  • quoが指す値は、x/yで得られる商と下位数ビットが等しく、x/yで得られた符号と同じ符号を持つ。
    • 商と等しくなる有効なビット数nは、少なくとも3以上の処理系定義の値とされる。

yがゼロである場合、quoが指す値は未規定となる。またその際、定義域エラーを発生させるかゼロを返すかは、実装定義となる。定義域エラーが発生した際の挙動については、<cmath> を参照。

備考

remquo関数ではx/yの厳密な商を求めることはできない。 三角関数のような周期性をもつ数学関数の内部実装において、商の低次ビットを利用した引数の還元(argument reduction)操作で利用する。

  • C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された

#include <iostream>
#include <cmath>

void test(double x, double y)
{
  int quo = 0;
  double rem = std::remquo(x, y, &quo);
  std::cout << "remquo(" << x << ", " << y << ") = "
            << "quotient:" << quo
            << " remainder:" << rem << std::endl;
}

int main()
{
  test(5.0, 2.0);
  test(6.0, 4.0);
  test(6.3, 3.0);
  test(6.3, -3.0);
  test(-6.3, 3.0);
  test(-6.3, -3.0);
  test(6.3, 3.15);
  test(6.0, 2.0);
}

出力例

remquo(5, 2) = quotient:2 remainder:1
remquo(6, 4) = quotient:2 remainder:-2
remquo(6.3, 3) = quotient:2 remainder:0.3
remquo(6.3, -3) = quotient:-2 remainder:0.3
remquo(-6.3, 3) = quotient:-2 remainder:-0.3
remquo(-6.3, -3) = quotient:2 remainder:-0.3
remquo(6.3, 3.15) = quotient:2 remainder:0
remquo(6, 2) = quotient:3 remainder:0

バージョン

言語

  • C++11

処理系

参照