std:: nextafter, std:: nextafterf, std:: nextafterl, std:: nexttoward, std:: nexttowardf, std:: nexttowardl
|
ヘッダーで定義
<cmath>
|
||
| (1) | ||
|
float
nextafter
(
float
from,
float
to
)
;
double
nextafter
(
double
from,
double
to
)
;
|
(C++11以降)
(C++23まで) |
|
|
constexpr
/* floating-point-type */
nextafter
(
/* floating-point-type */
from,
|
(C++23以降) | |
|
float
nextafterf
(
float
from,
float
to
)
;
|
(2) |
(C++11以降)
(constexprはC++23以降) |
|
long
double
nextafterl
(
long
double
from,
long
double
to
)
;
|
(3) |
(C++11以降)
(constexprはC++23以降) |
| (4) | ||
|
float
nexttoward
(
float
from,
long
double
to
)
;
double
nexttoward
(
double
from,
long
double
to
)
;
|
(C++11以降)
(C++23まで) |
|
|
constexpr
/* floating-point-type */
nexttoward
(
/* floating-point-type */
from,
|
(C++23以降) | |
|
float
nexttowardf
(
float
from,
long
double
to
)
;
|
(5) |
(C++11以降)
(constexpr C++23以降) |
|
long
double
nexttowardl
(
long
double
from,
long
double
to
)
;
|
(6) |
(C++11以降)
(constexpr C++23以降) |
|
ヘッダーで定義
<cmath>
|
||
|
template
<
class
Arithmetic1,
class
Arithmetic2
>
/* common-floating-point-type */
|
(A) |
(C++11以降)
(constexpr C++23以降) |
|
template
<
class
Integer
>
double nexttoward ( Integer from, long double to ) ; |
(B) |
(C++11以降)
(constexpr C++23以降) |
from の方向における to への次の表現可能な値を返します。
std::nextafter
のオーバーロードを提供する。
(C++23以降)
|
ライブラリは、
from
パラメータの型として、すべてのcv修飾されていない浮動小数点型に対する
|
(C++23以降) |
std::nextafter
オーバーロードが、他のすべての算術型の組み合わせに対して提供されます。
std::nexttoward
オーバーロードが全ての整数型に対して提供されており、これらは
double
として扱われます。
目次 |
パラメータ
| from, to | - | 浮動小数点または整数値 |
戻り値
エラーが発生しない場合、 from から to の方向における次の表現可能な値が返されます。 from が to と等しい場合、 to が返されます。
オーバーフローによる範囲エラーが発生した場合、
±HUGE_VAL
、
±HUGE_VALF
または
±HUGE_VALL
が返されます(
from
と同じ符号で)。
アンダーフローによる範囲エラーが発生した場合、正しい結果が返されます。
エラーハンドリング
エラーは math_errhandling で指定された通りに報告されます。
IEEE浮動小数点演算(IEC 60559)を実装がサポートしている場合、
- from が有限値であるが、期待される結果が無限大の場合、 FE_INEXACT および FE_OVERFLOW を発生させる。
- from が to と等しくなく、結果が非正規化数またはゼロの場合、 FE_INEXACT および FE_UNDERFLOW を発生させる。
- いずれの場合も、返される値は現在の丸めモードに依存しない。
- from または to のいずれかがNaNの場合、NaNが返される。
注記
POSIXは オーバーフローおよびアンダーフロー条件を範囲エラー( errno が設定される可能性がある)として指定しています。
IEC 60559は、 from が返されることを推奨しています。 from == to の場合です。これらの関数は代わりに to を返し、ゼロ周辺の動作を一貫させます: std :: nextafter ( - 0.0 , + 0.0 ) は + 0.0 を返し、 std :: nextafter ( + 0.0 , - 0.0 ) は - 0.0 を返します。
std::nextafter
は通常、IEEE表現の操作によって実装されます(
glibc
、
musl
)。
追加の
std::nextafter
オーバーロードは
(A)
と完全に同一である必要はありません。これらのオーバーロードは、第一引数
num1
と第二引数
num2
に対して以下を保証するのに十分であればよいのです:
|
(C++23まで) |
|
num1
と
num2
が算術型を持つ場合、
std
::
nextafter
(
num1, num2
)
は
std
::
nextafter
(
static_cast
<
/*common-floating-point-type*/
>
(
num1
)
,
そのような最大のランクとサブランクを持つ浮動小数点型が存在しない場合、 オーバーロード解決 は提供されたオーバーロードから使用可能な候補を結果として生成しない。 |
(C++23以降) |
追加の
std::nexttoward
オーバーロードは
(B)
と完全に同一である必要はありません。これらのオーバーロードは、整数型の引数
num
に対して、
std
::
nexttoward
(
num
)
の効果が
std
::
nexttoward
(
static_cast
<
double
>
(
num
)
)
と同じであることを保証するのに十分であればよいのです。
例
#include <cfenv> #include <cfloat> #include <cmath> #include <concepts> #include <iomanip> #include <iostream> int main() { float from1 = 0, to1 = std::nextafter(from1, 1.f); std::cout << "次の表現可能な浮動小数点数は " << std::setprecision(20) << from1 << " は " << to1 << std::hexfloat << " (" << to1 << ")\n" << std::defaultfloat; float from2 = 1, to2 = std::nextafter(from2, 2.f); std::cout << "次の表現可能な浮動小数点数は " << from2 << " は " << to2 << std::hexfloat << " (" << to2 << ")\n" << std::defaultfloat; double from3 = std::nextafter(0.1, 0), to3 = 0.1; std::cout << "数値 0.1 は2つの有効な倍精度浮動小数点数の間に位置します:\n" << std::setprecision(56) << " " << from3 << std::hexfloat << " (" << from3 << ')' << std::defaultfloat (注:HTMLタグと属性は翻訳せず、元のフォーマットを保持しています。、<< "\nおよび " << to3 << std::hexfloat << " (" << to3 << ")\n" << std::defaultfloat << std::setprecision(20); std::cout << "\nnextafterとnexttowardの違い:\n"; long double dir = std::nextafter(from1, 1.0L); // 最初のサブノーマル long double float x = std::nextafter(from1, dir); // 最初に dir を float に変換し、0 を与える std::cout << "nextafterを使用すると、次の浮動小数点数は「" << from1 << " は " << x << '\n'; x = std::nexttoward(from1, dir); std::cout << "nexttowardを使用すると、次の浮動小数点数は " << from1 << " は " << x << '\n'; std::cout << "\n特殊な値:\n"; { // #pragma STDC FENV_ACCESS ON std::feclearexcept(FE_ALL_EXCEPT); double from4 = DBL_MAX, to4 = std::nextafter(from4, INFINITY); std::cout << "次の表現可能なdouble値は" << std::setprecision(6) << from4 << std::hexfloat << " (" << from4 << ')' << std::defaultfloat << " は " << to4 << std::hexfloat << " (" << to4 << ")\n" << std::defaultfloat; if (std::fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW が発生しました\n"; if (std::fetestexcept(FE_INEXACT)) std::cout << " FE_INEXACTが発生しました\n"; } // FENV_ACCESS ブロック終了 float from5 = 0.0, to5 = std::nextafter(from5, -0.0); std::cout << "std::nextafter(+0.0, -0.0) は " << std::fixed << to5 << '\n'; auto precision_loss_demo = []<std::floating_point Fp>(const auto rem, const Fp start) { std::cout << rem; for (Fp from = start, to, Δ; (Δ = (to = std::nextafter(from, +INFINITY)) - from) < Fp(10.0); from *= Fp(10.0)) std::cout << "nextafter(" << std::scientific (注:指定された条件により、HTMLタグ・属性は翻訳せず、、タグ内のテキストも翻訳対象外のため、元の英語表記を維持しています。C++固有の用語「std::scientific」は翻訳していません)<< std::setprecision(0) << from << ", INF) は " << std::fixed << std::setprecision(6) << to << "; Δ = " << Δ << '\n'; }; precision_loss_demo("\nfloatの精度損失デモ:\n", 10.0f); precision_loss_demo("\ndoubleの精度損失デモ:\n", 10.0e9); precision_loss_demo("\nlong doubleの精度損失デモ:\n", 10.0e17L); }
出力:
0の次の表現可能なfloatは1.4012984643248170709e-45 (0x1p-149)
1の次の表現可能なfloatは1.0000001192092895508 (0x1.000002p+0)
数値0.1は2つの有効なdoubleの間に位置する:
0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4)
および 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4)
nextafterとnexttowardの違い:
nextafterでは、0の次のfloatは0
nexttowardでは、0の次のfloatは1.4012984643248170709e-45
特殊な値:
1.79769e+308 (0x1.fffffffffffffp+1023)の次の表現可能なdoubleはinf (inf)
FE_OVERFLOWを発生
FE_INEXACTを発生
std::nextafter(+0.0, -0.0)は-0.000000を返す
floatの精度損失デモ:
nextafter(1e+01, INF)は10.000001を返す;Δ = 0.000001
nextafter(1e+02, INF)は100.000008を返す;Δ = 0.000008
nextafter(1e+03, INF)は1000.000061を返す;Δ = 0.000061
nextafter(1e+04, INF)は10000.000977を返す;Δ = 0.000977
nextafter(1e+05, INF)は100000.007812を返す;Δ = 0.007812
nextafter(1e+06, INF)は1000000.062500を返す;Δ = 0.062500
nextafter(1e+07, INF)は10000001.000000を返す;Δ = 1.000000
nextafter(1e+08, INF)は100000008.000000を返す;Δ = 8.000000
doubleの精度損失デモ:
nextafter(1e+10, INF)は10000000000.000002を返す;Δ = 0.000002
nextafter(1e+11, INF)は100000000000.000015を返す;Δ = 0.000015
nextafter(1e+12, INF)は1000000000000.000122を返す;Δ = 0.000122
nextafter(1e+13, INF)は10000000000000.001953を返す;Δ = 0.001953
nextafter(1e+14, INF)は100000000000000.015625を返す;Δ = 0.015625
nextafter(1e+15, INF)は1000000000000000.125000を返す;Δ = 0.125000
nextafter(1e+16, INF)は10000000000000002.000000を返す;Δ = 2.000000
long doubleの精度損失デモ:
nextafter(1e+18, INF)は1000000000000000000.062500を返す;Δ = 0.062500
nextafter(1e+19, INF)は10000000000000000001.000000を返す;Δ = 1.000000
nextafter(1e+20, INF)は100000000000000000008.000000を返す;Δ = 8.000000
関連項目
|
Cドキュメント
を参照
nextafter
|