nextafter() - nextafterl() - nexttoward() - nexttowardl() — 表現可能な次の浮動小数点値の計算

形式

#include <math.h>
double nextafter(double x, double y); 
long double nextafterl(long double x, long double y); 
double nexttoward(double x, long double y); 
long double nexttowardl(long double x, long double y);

言語レベル

ANSI

スレッド・セーフ

はい

説明

nextafter()nextafterl()nexttoward()、および nexttowardl() 関数は、y の方向で x の後の次の表現可能な値を計算します。

戻り値

nextafter()nextafterl()nexttoward()、および nexttowardl() 関数は、x の後の次の表現可能な値を y の方向に戻します。 x が y に等しい場合は、yを戻します。 x または y が NaN ( 数値ではない ) の場合、NaN が戻され、errno が EDOM に設定されます。 x が最大有限値で、結果が無限または表現可能ではない場合は、HUGE_VAL が戻され、errno に ERANGE が設定されます。

この例では、浮動小数点の値を次のより大きな表現可能な値、および次のより小さな表現可能な値に変換します。 変換した値は出力されます。
#include <stdio.h> 
#include <math.h>
int main(void)
{
 double x, y; 
 long double ld;

 x = nextafter(1.234567899, 10.0);
 printf("nextafter 1.234567899 is %#19.17g\n" x);
 ld = nextafterl(1.234567899, -10.0);
 printf("nextafterl 1.234567899 is %#19.17g\n" ld);

 x = nexttoward(1.234567899, 10.0);
 printf("nexttoward 1.234567899 is %#19.17g\n" x);
 ld = nexttowardl(1.234567899, -10.0);
 printf("nexttowardl 1.234567899 is %#19.17g\n" ld);

} 

/***************** Output should be similar to: ***************** 
nextafter 1.234567899 is  1.2345678990000002
nextafterl 1.234567899 is 1.2345678989999997
nexttoward 1.234567899 is 1.2345678990000002
nexttowardl 1.234567899 is 1.2345678989999997

*/

関連情報