名前空間
変種
操作

「cpp/numeric/math/remquo」の版間の差分

提供: cppreference.com
< cpp‎ | numeric‎ | math
(Translated from the English version using Google Translate)
 
 
(3人の利用者による、間の10版が非表示)
1行: 1行:
{{tr_note}}
+
{{cpp/title|remquo}}
{{cpp/title|remquo}}
+
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
{{ddcl list begin}}
+
{{begin}}
{{ddcl list header | cmath}}
+
{{header | cmath}}
{{ddcl list item | notes={{mark since c++11}} |num=1|
+
{{| sincec++11 |num=1|
float      remquo( float x, float y, int *quo );
+
float      remquo
 +
( float x, float y, int* quo );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |num=2|
+
{{| sincec++11 |num=2|
double      remquo( double x, double y, int *quo );
+
double      remquo ( double x, double y, int* quo );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |num=3|
+
{{| sincec++11 |num=3|
long double remquo( long double x, long double y, int *quo );
+
long double remquo
 +
( long double x, long double y, int* quo );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} | num=4|
+
{{| sincec++11 | num=4|
Promoted   remquo( Arithmetic x, Arithmetic y, int *quo );
+
  remquo ( x, y, int* quo );
 
}}
 
}}
{{ddcl list end}}
+
{{end}}
  
{{tr|いくつかのビットを格納する値が{{c|x/y}}関数が行うように、浮動小数点除算演算{{rlpt|remainder}}の余りを計算します。また、記号と{{c|x/y}}の最後のビットのうちの少なくとも3が{{c|quo}}.|Computes the remainder of the floating point division operation {{c|x/y}} as the {{rlpt|remainder}} function does.  Additionally, the sign and at least the three of the last bits of {{c|x/y}} will be stored in {{c|quo}}.}}
+
{{|{{c|x/y}} {{c|x/y}} {{c|quo}}
 +
|{{c|}} {{|}} {{c|}} {{c|}}
  
4) {{tr|いずれかの引数が整数型を持つ場合、それは{{c|double}}にキャストされます。他の引数は{{c|long double}}あれば、戻り値の型は{{c|long double}}、それ以外の場合は{{c|double}}です|If any argument has integral type, it is cast to {{c|double}}. If any other argument is {{c|long double}}, then the return type is {{c|long double}}, otherwise it is {{c|double}}}}
+
======
===パラメータ===
+
{{begin}}
{{param list begin}}
+
{{| x, y | }}
{{param list item | x, y |{{tr| 浮動小数点値| floating point values}}}}
+
{{| quo | {{c|x/y}} }}
{{param list item | quo |{{tr| 整数へのポインタの{{c|x/y}}インチ | pointer to an integer value to store some bits of {{c|x/y}} in }}}}
+
{{end}}
{{param list end}}
+
  
===値を返します===
+
======
{{tr|割っ残りの引数|remainder of dividing arguments}}
+
{{|remainder}}
 +
 
 +
 +
 
 +
 +
 
 +
 +
 
 +
 +
 
 +
 +
 +
 
 +
 +
 
 +
 +
 +
 +
 +
 +
  
 
===ノート===
 
===ノート===
{{tr|、非常に大規模な{{tt|sin(πx)}}ため{{tt|x}}を計算するとき、{{c|std::sin}}を直接呼び出す方法は大きな誤差になる場合がありますが、関数の引数は、最初{{tt|std::remquo}}で還元されている場合:この関数は、浮動小数点値として正確に表現周期で周期関数を実装する際に便利です商の下位ビットは符号を決定するために使用することができ、残りは高精度で値を計算するために使用されるかもしれません.|This function is useful when implementing periodic functions with the period exactly representable as a floating-point value: when calculating {{tt|sin(πx)}} for a very large {{tt|x}}, calling {{c|std::sin}} directly may result in a large error, but if the function argument is first reduced with {{tt|std::remquo}}, the low-order bits of the quotient may be used to determine the sign and the remainder may be used to calculate the value with high precision.}}
+
{{|{{tt|}}
 +
 
 +
{{tt|x}} を計算するとき、 {{|std::sin}} {{tt|std::remquo}}
 +
 
 +
{{tt|}}
 +
 
 +
 +
{{|
 +
 +
 +
 +
 
 +
 +
 +
x}
 +
,
 +
 +
{
 +
 +
 +
 +
{
 +
std::
 +
 +
}
 +
}
 +
 +
{
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
std::remquo,
 +
 +
 +
 +
 +
 +
-
 +
 +
 +
 +
 +
 +
 +
 +
and the
 +
 +
 +
 +
 
 +
 +
 +
 +
 +
remainder
 +
 +
}}
  
===も参照してください===
+
{{|||}}
{{dcl list begin}}
+
{{dcl list template | cpp/numeric/math/dcl list remainder}}
+
{{dcl list template | cpp/numeric/math/dcl list fmod}}
+
{{dcl list template | cpp/numeric/math/dcl list div}}
+
{{dcl list end}}
+

2018年12月5日 (水) 14:58時点における最新版

 
 
 
一般的な数学関数
関数
基本的な演算
(C++11)
remquo
(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> で定義
float       remquo ( float x, float y, int* quo );
float       remquof( float x, float y, int* quo );
(1) (C++11以上)
double      remquo ( double x, double y, int* quo );
(2) (C++11以上)
long double remquo ( long double x, long double y, int* quo );
long double remquol( long double x, long double y, int* quo );
(3) (C++11以上)
昇格後の型    remquo ( 算術型1 x, 算術型2 y, int* quo );
(4) (C++11以上)
1-3) std::remainder() 関数が行うように除算 x/y の浮動小数点剰余を計算します。 さらに、周期内の結果の象限を判定するのに十分な、 x/y の少なくとも3個の最後のビットと符号が quo に格納されます。
4) (1-3) でカバーされない算術型の引数のすべての組み合わせに対するオーバーロード集合または関数テンプレート。 いずれかの非ポインタ引数が整数型の場合、それは double にキャストされます。 他のいずれかの非ポインタ引数が long double の場合、戻り値型は long double になり、そうでなければ double になります。

目次

[編集] 引数

x, y - 浮動小数点数
quo - x/y の符号といくつかのビットを格納するための整数値を指すポインタ

[編集] 戻り値

成功した場合、 std::remainder で定義されている通りの除算 x/y の浮動小数点剰余を返し、 x/y の符号と少なくとも3個の最下位ビットを *quo に格納します (形式的には、符号が x/y の符号で、絶対値が x/y の整数の商の絶対値と modulo 2n
で合同な値を格納します。 ただし n3 より大きいまたは等しい処理系定義の整数です)。

y がゼロの場合、 *quo に格納される値は未規定です。

定義域エラーが発生した場合、処理系定義の値 (サポートされていれば NaN) が返されます。

アンダーフローによる値域エラーが発生した場合、非正規化数がサポートされていれば正しい値が返されます。

y がゼロだけれども定義域エラーが発生しない場合、ゼロが返されます。

[編集] エラー処理

math_errhandling で規定されている通りにエラーが報告されます。

y がゼロの場合は、定義域エラーが発生することがあります。

処理系が IEEE 浮動小数点算術 (IEC 60559) をサポートしている場合、

  • 現在の丸めモードは効果を持ちません。
  • FE_INEXACT が発生することはありません。
  • x が ±∞ であり y が NaN でなければ、 NaN が返され、 FE_INVALID が発生します。
  • y が ±0 であり x が NaN でなければ、 NaN が返され、 FE_INVALID が発生します。
  • x または y のいずれかが NaN であれば、 NaN が返されます。

[編集] ノート

POSIX は、 x が無限大または y がゼロの場合、定義域エラーが発生することを要求しています

この関数は、浮動小数点値として正確に表現可能な周期を持つ周期関数を実装するときに便利です。 非常に大きな x について sin(πx) を計算するとき、 std::sin を直接呼ぶと大きな誤差が発生する可能性がありますが、まず引数を std::remquo で縮小すれば、商の下位ビットを周期内の結果の象限と符号を決めるために使用することができ、余りを高精度な値を計算するために使用することができます。

プラットフォームによっては、この演算はハードウェアでサポートされています (例えば Intel CPU では、 FPREM1 は完了時にちょうど3ビットの精度の商を残します)。

[編集]

#include <iostream>
#include <cmath>
#include <cfenv>
 
#pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1);
double cos_pi_x_naive(double x) { return std::cos(pi * x); }
// 周期は2、値は (0;0.5) は正、 (0.5;1.5) は負、 (1.5,2) は正
double cos_pi_x_smart(double x)
{
    int quadrant;
    double rem = std::remquo(x, 1, &quadrant);
    quadrant = (unsigned)quadrant % 2;  // 周期は2
    switch(quadrant) {
        case 0: return std::cos(pi * rem);
        case 1: return -std::cos(pi * rem);
    };
}
int main()
{
    std::cout << "cos(pi * 0.25) = " << cos_pi_x_naive(0.25) << '\n'
              << "cos(pi * 1.25) = " << cos_pi_x_naive(1.25) << '\n'
              << "cos(pi * 2.25) = " << cos_pi_x_naive(2.25) << '\n'
              << "cos(pi * 0.25) = " << cos_pi_x_smart(0.25) << '\n'
              << "cos(pi * 1.25) = " << cos_pi_x_smart(1.25) << '\n'
              << "cos(pi * 2.25) = " << cos_pi_x_smart(2.25) << '\n'
              << "cos(pi * 1000000000000.25) = "
              << cos_pi_x_naive(1000000000000.25) << '\n'
              << "cos(pi * 1000000000001.25) = "
              << cos_pi_x_naive(1000000000001.25) << '\n'
              << "cos(pi * 1000000000000.25) = "
              << cos_pi_x_smart(1000000000000.25) << '\n'
              << "cos(pi * 1000000000001.25) = "
              << cos_pi_x_smart(1000000000001.25) << '\n';
    // エラー処理
    std::feclearexcept(FE_ALL_EXCEPT);
    int quo;
    std::cout << "remquo(+Inf, 1) = " << std::remquo(INFINITY, 1, &quo) << '\n';
    if(fetestexcept(FE_INVALID)) std::cout << "    FE_INVALID raised\n";
}

出力例:

cos(pi * 0.25) = 0.707107
cos(pi * 1.25) = -0.707107
cos(pi * 2.25) = 0.707107
cos(pi * 0.25) = 0.707107
cos(pi * 1.25) = -0.707107
cos(pi * 2.25) = 0.707107
cos(pi * 1000000000000.25) = 0.707123
cos(pi * 1000000000001.25) = -0.707117
cos(pi * 1000000000000.25) = 0.707107
cos(pi * 1000000000001.25) = -0.707107 
remquo(+Inf, 1) = -nan
    FE_INVALID raised

[編集] 関連項目

整数除算の商と余りを計算します
(関数) [edit]
(C++11)(C++11)
浮動小数点除算の余りを計算します
(関数) [edit]
(C++11)(C++11)(C++11)
除算の符号付きの余りを計算します
(関数) [edit]