「cpp/numeric/math/remquo」の版間の差分
(1人の利用者による、間の4版が非表示) | |||
1行: | 1行: | ||
− | {{cpp/title|remquo}} | + | {{cpp/title|remquo}} |
{{cpp/numeric/math/navbar}} | {{cpp/numeric/math/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | cmath}} | {{dcl header | cmath}} | ||
{{dcl | since=c++11 |num=1| | {{dcl | since=c++11 |num=1| | ||
− | float remquo( float x, float y, int* quo ); | + | float remquo |
+ | ( float x, float y, int* quo ); | ||
}} | }} | ||
{{dcl | since=c++11 |num=2| | {{dcl | since=c++11 |num=2| | ||
− | double remquo( double x, double y, int* quo ); | + | double remquo ( double x, double y, int* quo ); |
}} | }} | ||
{{dcl | since=c++11 |num=3| | {{dcl | since=c++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 ); | ||
}} | }} | ||
{{dcl | since=c++11 | num=4| | {{dcl | since=c++11 | num=4| | ||
− | + | remquo ( x, y, int* quo ); | |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
@1-3@ {{lc|std::remainder()}} 関数が行うように除算 {{c|x/y}} の浮動小数点剰余を計算します。 さらに、周期内の結果の象限を判定するのに十分な、 {{c|x/y}} の少なくとも3個の最後のビットと符号が {{c|quo}} に格納されます。 | @1-3@ {{lc|std::remainder()}} 関数が行うように除算 {{c|x/y}} の浮動小数点剰余を計算します。 さらに、周期内の結果の象限を判定するのに十分な、 {{c|x/y}} の少なくとも3個の最後のビットと符号が {{c|quo}} に格納されます。 | ||
− | @4@ 1-3 でカバーされない[[cpp/types/is_arithmetic|算術型]]の引数のすべての組み合わせに対するオーバーロード集合または関数テンプレート。 いずれかの非ポインタ引数が[[cpp/types/is_integral|整数型]]の場合、それは {{c|double}} にキャストされます。 他のいずれかの非ポインタ引数が {{c|long double}} の場合、戻り値型は {{c|long double}} になり、そうでなければ {{c|double}} になります。 | + | @4@ 1-3でカバーされない[[cpp/types/is_arithmetic|算術型]]の引数のすべての組み合わせに対するオーバーロード集合または関数テンプレート。 いずれかの非ポインタ引数が[[cpp/types/is_integral|整数型]]の場合、それは {{c|double}} にキャストされます。 他のいずれかの非ポインタ引数が {{c|long double}} の場合、戻り値型は {{c|long double}} になり、そうでなければ {{c|double}} になります。 |
===引数=== | ===引数=== | ||
38行: | 40行: | ||
===エラー処理=== | ===エラー処理=== | ||
− | + | |math_errhandlingで規定されている通りにエラーが報告されます。 | |
{{tt|y}} がゼロの場合は、定義域エラーが発生することがあります。 | {{tt|y}} がゼロの場合は、定義域エラーが発生することがあります。 | ||
50行: | 52行: | ||
===ノート=== | ===ノート=== | ||
− | [http://pubs.opengroup.org/onlinepubs/9699919799/functions/remquo.html | + | [http://pubs.opengroup.org/onlinepubs/9699919799/functions/remquo.html ] |
この関数は、浮動小数点値として正確に表現可能な周期を持つ周期関数を実装するときに便利です。 非常に大きな {{tt|x}} について {{math|sin(πx)}} を計算するとき、 {{lc|std::sin}} を直接呼ぶと大きな誤差が発生する可能性がありますが、まず引数を {{tt|std::remquo}} で縮小すれば、商の下位ビットを周期内の結果の象限と符号を決めるために使用することができ、余りを高精度な値を計算するために使用することができます。 | この関数は、浮動小数点値として正確に表現可能な周期を持つ周期関数を実装するときに便利です。 非常に大きな {{tt|x}} について {{math|sin(πx)}} を計算するとき、 {{lc|std::sin}} を直接呼ぶと大きな誤差が発生する可能性がありますが、まず引数を {{tt|std::remquo}} で縮小すれば、商の下位ビットを周期内の結果の象限と符号を決めるために使用することができ、余りを高精度な値を計算するために使用することができます。 | ||
65行: | 67行: | ||
const double pi = std::acos(-1); | const double pi = std::acos(-1); | ||
double cos_pi_x_naive(double x) { return std::cos(pi * x); } | double cos_pi_x_naive(double x) { return std::cos(pi * x); } | ||
− | // | + | // (0;0.5) (0.5;1.5) (1.5,2) |
double cos_pi_x_smart(double x) | double cos_pi_x_smart(double x) | ||
{ | { | ||
int quadrant; | int quadrant; | ||
double rem = std::remquo(x, 1, &quadrant); | double rem = std::remquo(x, 1, &quadrant); | ||
− | quadrant = (unsigned)quadrant % 2; // | + | quadrant = (unsigned)quadrant % 2; // |
switch(quadrant) { | switch(quadrant) { | ||
case 0: return std::cos(pi * rem); | case 0: return std::cos(pi * rem); | ||
92行: | 94行: | ||
<< "cos(pi * 1000000000001.25) = " | << "cos(pi * 1000000000001.25) = " | ||
<< cos_pi_x_smart(1000000000001.25) << '\n'; | << cos_pi_x_smart(1000000000001.25) << '\n'; | ||
− | // | + | // |
std::feclearexcept(FE_ALL_EXCEPT); | std::feclearexcept(FE_ALL_EXCEPT); | ||
int quo; | int quo; | ||
122行: | 124行: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | deenesfritptruzh | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
2018年12月5日 (水) 14:58時点における最新版
ヘッダ <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以上) |
目次 |
[編集] 引数
x, y | - | 浮動小数点数 |
quo | - | x/y の符号といくつかのビットを格納するための整数値を指すポインタ |
[編集] 戻り値
成功した場合、 std::remainder で定義されている通りの除算 x/y の浮動小数点剰余を返し、 x/y の符号と少なくとも3個の最下位ビットを *quo に格納します (形式的には、符号が x/y の符号で、絶対値が x/y の整数の商の絶対値と modulo 2n
で合同な値を格納します。 ただし n は 3 より大きいまたは等しい処理系定義の整数です)。
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
[編集] 関連項目
(C++11) |
整数除算の商と余りを計算します (関数) |
(C++11)(C++11) |
浮動小数点除算の余りを計算します (関数) |
(C++11)(C++11)(C++11) |
除算の符号付きの余りを計算します (関数) |
remquo の C言語リファレンス
|