nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl
提供: cppreference.com
ヘッダ <math.h> で定義 | ||
float nextafterf(float from, float to ); | (1) | (C99以上) |
double nextafter(double from, double to ); | (2) | (C99以上) |
longdouble nextafterl(longdouble from, longdouble to ); | (3) | (C99以上) |
float nexttowardf(float from, longdouble to ); | (4) | (C99以上) |
double nexttoward(double from, longdouble to ); | (5) | (C99以上) |
longdouble nexttowardl(longdouble from, longdouble to ); | (6) | (C99以上) |
ヘッダ <tgmath.h> で定義 | ||
#define nextafter(from, to) | (7) | (C99以上) |
#define nexttoward(from, to) | (8) | (C99以上) |
1-3) まず、両方の引数が関数の型に変換されます。 そして、
to
の方向の from
の次の表現可能な値が返されます。 from
が to
と等しい場合は、 to
が返されます。4-6) まず、第1引数が関数の型に変換されます。 そして、
to
の方向の from
の次に表現可能な値が返されます。 from
が to
と等しい場合は、範囲や精度を失わずに longdouble から戻り値の型に変換された to
が返されます。7) 型総称マクロ。 任意の引数が longdouble の場合は
nextafterl
が呼ばれます。 そうでなく、任意の引数が整数型または double 型の場合は nextafter
が呼ばれます。 そうでなければ nextafterf
が呼ばれます。8) 型総称マクロ。 引数
from
が longdouble 型の場合は nexttowardl
が呼ばれます。 そうでなく、 from
が整数型または double 型の場合は nexttoward
が呼ばれます。 そうでなければ nexttowardf
が呼ばれます。目次 |
[編集]引数
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
を返しますが、これはゼロ周りの動作に一貫性を持たせます。 nextafter(-0.0, +0.0)
は +0.0
を返し、 nextafter(+0.0, -0.0)
は –0.0
を返します。
[編集]例
Run this code
#include <math.h>#include <stdio.h>#include <float.h>#include <fenv.h> int main(void){float from1 =0, to1 = nextafterf(from1, 1);printf("The next representable float after %.2f is %.20g (%a)\n", from1, to1, to1); float from2 =1, to2 = nextafterf(from2, 2);printf("The next representable float after %.2f is %.20f (%a)\n", from2, to2, to2); double from3 = nextafter(0.1, 0), to3 =0.1;printf("The number 0.1 lies between two valid doubles:\n"" %.56f (%a)\nand %.55f (%a)\n", from3, from3, to3, to3); // difference between nextafter and nexttoward:longdouble dir = nextafterl(from1, 1);// first subnormal long doublefloat x = nextafterf(from1, dir);// first converts dir to float, giving 0printf("Using nextafter, next float after %.2f (%a) is %.20g (%a)\n", from1, from1, x, x); x = nexttowardf(from1, dir);printf("Using nexttoward, next float after %.2f (%a) is %.20g (%a)\n", from1, from1, x, x); // special values{#pragma STDC FENV_ACCESS ONfeclearexcept(FE_ALL_EXCEPT);double from4 =DBL_MAX, to4 = nextafter(from4, INFINITY);printf("The next representable double after %.2g (%a) is %.23f (%a)\n", from4, from4, to4, to4);if(fetestexcept(FE_OVERFLOW))puts(" raised FE_OVERFLOW");if(fetestexcept(FE_INEXACT))puts(" raised FE_INEXACT");}// end FENV_ACCESS block float from5 =0.0, to5 = nextafter(from5, -0.0);printf("nextafter(+0.0, -0.0) gives %.2g (%a)\n", to5, to5);}
出力:
The next representable float after 0.00 is 1.4012984643248170709e-45 (0x1p-149) The next representable float after 1.00 is 1.00000011920928955078 (0x1.000002p+0) The number 0.1 lies between two valid doubles: 0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4) and 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4) Using nextafter, next float after 0.00 (0x0p+0) is 0 (0x0p+0) Using nexttoward, next float after 0.00 (0x0p+0) is 1.4012984643248170709e-45 (0x1p-149) The next representable double after 1.8e+308 (0x1.fffffffffffffp+1023) is inf (inf) raised FE_OVERFLOW raised FE_INEXACT nextafter(+0.0, -0.0) gives -0 (-0x0p+0)
[編集]参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.12.11.3 The nextafter functions (p: 256)
- 7.12.11.4 The nexttoward functions (p: 257)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.8.3 The nextafter functions (p: 529)
- F.10.8.4 The nexttoward functions (p: 529)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.11.3 The nextafter functions (p: 237)
- 7.12.11.4 The nexttoward functions (p: 238)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.8.3 The nextafter functions (p: 466)
- F.9.8.4 The nexttoward functions (p: 466)
[編集]関連項目
nextafter の C++リファレンス |