std::nextafter, std::nextafterf, std::nextafterl, std::nexttoward, std::nexttowardf, std::nexttowardl
提供: cppreference.com
ヘッダ <cmath> で定義
|
||
float nextafter ( float from, float to ); float nextafterf( float from, float to ); |
(1) | (C++11以上) |
double nextafter ( double from, double to ); |
(2) | (C++11以上) |
long double nextafter ( long double from, long double to ); long double nextafterl( long double from, long double to ); |
(3) | (C++11以上) |
昇格後の型 nextafter ( 算術型1 from, 算術型2 to ); |
(4) | (C++11以上) |
float nexttoward ( float from, long double to ); float nexttowardf( float from, long double to ); |
(5) | (C++11以上) |
double nexttoward ( double from, long double to ); |
(6) | (C++11以上) |
long double nexttoward ( long double from, long double to ); long double nexttowardl( long double from, long double to ); |
(7) | (C++11以上) |
double nexttoward ( 整数型 from, long double to ); |
(8) | (C++11以上) |
to
の方向の from
の次の表現可能な値を返します。
1-3)
from
が to
と等しい場合は、 to
が返されます。5-7)
from
が to
と等しい場合は、範囲や精度を失わずに long double から関数の戻り値の型に変換された to
が返されます。4) (1-3) でカバーされない算術型の引数のすべての組み合わせに対するオーバーロード集合または関数テンプレート。 任意の引数が 整数型の場合、それは double に変換されます。 任意の引数が long double の場合、戻り値型も long double になり、そうでなければ戻り値型は必ず double になります。
目次 |
[編集] 引数
from, to | - | 浮動小数点値 |
[編集] 戻り値
エラーが発生しなければ、 to
の方向の from
の次の表現可能な値が返されます。 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==to
であれば常に from
が返されることを推奨しています。 これらの関数は代わりに to
を返しますが、これはゼロ周りの動作に一貫性を持たせます。 std::nextafter(-0.0, +0.0)
は +0.0
を返し、 std::nextafter(+0.0, -0.0)
は –0.0
を返します。
[編集] 例
Run this code
#include <cmath> #include <iomanip> #include <iostream> #include <cfloat> #include <cfenv> int main() { float from1 = 0, to1 = std::nextafter(from1, 1.f); std::cout << "The next representable float after " << std::setprecision(20) << from1 << " is " << to1 << std::hexfloat << " (" << to1 << ")\n" << std::defaultfloat; float from2 = 1, to2 = std::nextafter(from2, 2.f); std::cout << "The next representable float after " << from2 << " is " << to2 << std::hexfloat << " (" << to2 << ")\n" << std::defaultfloat; double from3 = std::nextafter(0.1, 0), to3 = 0.1; std::cout << "The number 0.1 lies between two valid doubles:\n" << std::setprecision(56) << " " << from3 << std::hexfloat << " (" << from3 << ')' << std::defaultfloat << "\nand " << to3 << std::hexfloat << " (" << to3 << ")\n" << std::defaultfloat << std::setprecision(20); // nextafter と nexttoward の差: long double dir = std::nextafter(from1, 1.0L); // 最初の非正規化数の long double float x = std::nextafter(from1, dir); // dir を float に変換 (ゼロになります) std::cout << "With nextafter, next float after " << from1 << " is " << x << '\n'; x = std::nexttoward(from1, dir); std::cout << "With nexttoward, next float after " << from1 << " is " << x << '\n'; // 特殊な値 { #pragma STDC FENV_ACCESS ON std::feclearexcept(FE_ALL_EXCEPT); double from4 = DBL_MAX, to4 = std::nextafter(from4, INFINITY); std::cout << "The next representable double after " << std::setprecision(6) << from4 << std::hexfloat << " (" << from4 << ')' << std::defaultfloat << " is " << to4 << std::hexfloat << " (" << to4 << ")\n" << std::defaultfloat; if(std::fetestexcept(FE_OVERFLOW)) std::cout << " raised FE_OVERFLOW\n"; if(std::fetestexcept(FE_INEXACT)) std::cout << " raised FE_INEXACT\n"; } // FENV_ACCESS ブロックの終わり float from5 = 0.0, to5 = std::nextafter(from5, -0.0); std::cout << "std::nextafter(+0.0, -0.0) gives " << std::fixed << to5 << '\n'; }
出力:
The next representable float after 0 is 1.4012984643248170709e-45 (0x1p-149) The next representable float after 1 is 1.0000001192092895508 (0x1.000002p+0) The number 0.1 lies between two valid doubles: 0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4) and 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4) With nextafter, next float after 0 is 0 With nexttoward, next float after 0 is 1.4012984643248170709e-45 The next representable double after 1.79769e+308 (0x1.fffffffffffffp+1023) is inf (inf) raised FE_OVERFLOW raised FE_INEXACT std::nextafter(+0.0, -0.0) gives -0.000000
[編集] 関連項目
nextafter の C言語リファレンス
|