吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C语言中的 nextafter() 和 nexttoward()

C语言中的 nextafter() 和 nexttoward()

来源:网络 | 更新时间:2022-02-08 07:49:34
您将如何解决 C/C++ 中的以下问题?
  • C/C++ 中最小的可表示正浮点数是多少?
  • C/C++ 中最大的可表示负浮点数是多少?
  • 给定一个正浮点数 x,找到小于 x 的最大可表示浮点值?
nextafter(x, y) 和 nexttoward(xy) 在 C 和 C++ 中,nextafter(x, y) 和 nexttoward(xy) 都是在 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 : %en",
nextafter(0.0, 1.0));
printf ("Largest negative floating point number :%en",
nextafter(0.0, -1.0));
printf ("Largest positive floating point number smaller than 0.5 : %en",
nextafter(0.5, 0.0));

// using nexttoward
printf ("Smallest positive floating point number : %en",
nexttoward(0.0, 1.0));
printf ("Largest negative floating point number : %en",
nexttoward(0.0, -1.0));
printf ("Largest positive floating point number smaller than 0.5 : %en",
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

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com