您將如何解決C /C++中的以下問題?
- C /C++中最小的可表示正浮點數是多少?
- C /C++中最大的可表示負浮點數是多少?
- 給定正浮點數x,找到小於x的最大可表示浮點值?
nextafter(x,y)和nexttoward(x.y)
在C和C++中,nextafter(x,y)和nexttoward(x.y)都是math.h或cmath頭文件中定義的相似函數。它們都在y方向上返回x之後的下一個可表示值。 nexttoward()具有更精確的第二個參數y。
以下程序演示了該概念:
// C program to demonstrate use of nextafter() and nexttoward()
#include <stdio.h>
#include <math.h>
int main ()
{
// using nextafter
printf ("Smallest positive floating point number:%e\n",
nextafter(0.0, 1.0));
printf ("Largest negative floating point number:%e\n",
nextafter(0.0, -1.0));
printf ("Largest positive floating point number smaller than 0.5:%e\n",
nextafter(0.5, 0.0));
// using nexttoward
printf ("Smallest positive floating point number:%e\n",
nexttoward(0.0, 1.0));
printf ("Largest negative floating point number:%e\n",
nexttoward(0.0, -1.0));
printf ("Largest positive floating point number smaller than 0.5:%e\n",
nexttoward(0.5, 0.0));
return (0);
}
輸出:
nextafter first value greater than zero:4.940656e-324 nextafter first value less than zero:-4.940656e-324 nexttoward first value greater than zero:4.940656e-324 nexttoward first valnextafter first value greater than zero:4.940656e-324 nextafter first value less than zero:-4.940656e-324 nexttoward first value greater than zero:4.940656e-324 nexttoward first value less than zero:-4.940656e-324 ue less than zero:-4.940656e-324
相關用法
注:本文由純淨天空篩選整理自 nextafter() and nexttoward() in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。