C语言函数大全
本篇介绍C语言函数大全-- n 开头的函数
1. nan
1.1 函数说明
函数声明 | 函数功能 |
---|---|
double nan(const char *tagp); |
用于返回一个表示 NaN(非数值)的 double 类型数字 |
参数:
- tagp : 指向字符串的指针;用于指定 NaN 数字的类型。如果不需要指定类型,则可以将该参数设置为 NULL。
1.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.0 / 0.0; // 使用除 0 运算符来生成 NaN 数字
printf("x: %f\n", x);
double y = nan(NULL); // 使用 nan() 函数来生成 NaN 数字
printf("y: %f\n", y);
return 0;
}
注意: NaN
数字具有一些特殊的属性,例如与任何数字进行比较都会返回 false
,因此在实际编程中需要特别小心处理 NaN
的情况,避免出现异常结果
1.3 运行结果
2. nanosleep
2.1 函数说明
函数声明 | 函数功能 |
---|---|
int nanosleep(const struct timespec *req, struct timespec *rem); |
用于暂停当前进程的执行一段指定的时间。相比于 sleep() 函数,nanosleep() 函数可以精确地指定等待时间,以纳秒为单位。 |
参数:
- req : 指向 timespec 结构体的指针,用于指定要等待的时间。
timespec
结构体包含两个成员变量:tv_sec
表示等待时间的整数部分(秒),tv_nsec
表示等待时间的小数部分(纳秒)。如果rem
参数不为NULL
,则在函数返回时,未完成的等待时间将被存储在rem
指向的timespec
结构体中。 - rem : 未完成的等待时间
2.2 演示示例
#include <stdio.h>
#include <time.h>
int main(void)
{
struct timespec req = { 0 };
req.tv_sec = 2; // 等待时间为 2 秒
req.tv_nsec = 5000000; // 加上 5 毫秒
int ret = nanosleep(&req, NULL);
if (ret == 0) {
printf("nanosleep completed\n");
} else {
printf("nanosleep interrupted by signal\n");
}
return 0;
}
在上述的程序中,
- 我们首先创建一个
timespec
结构体变量req
,用于指定等待时间。在本例中,我们将等待时间设置为2
秒加上5
毫秒。 - 接着,我们调用
nanosleep()
函数,并传递req
变量的地址作为第一个参数。如果函数执行成功(即完成了预定的等待时间),则返回值为0
,否则返回-1
。 - 最后,我们检查函数的返回值,以确定
nanosleep()
是否成功完成。如果返回值为0
,则表示函数已经完成了预定的等待时间;如果返回值为-1
,则说明函数被信号中断。在实际编程中,我们还可以通过检查errno
变量来获取更具体的错误信息。
3. nearbyint,nearbyintf,nearbyintl
3.1 函数说明
函数声明 | 函数功能 |
---|---|
double nearbyint(double x); |
用于将一个浮点数四舍五入到最接近的整数值(double) |
float nearbyintf(float x); |
用于将一个浮点数四舍五入到最接近的整数值(float) |
long double nearbyintl(long double x); |
用于将一个浮点数四舍五入到最接近的整数值(long double) |
3.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 2.3;
double y = -1.8;
double z = nearbyint(x); // 将 2.3 四舍五入到 2
double w = nearbyint(y); // 将 -1.8 四舍五入到 -2
printf("x: %lf, nearbyint(x): %lf\n", x, z);
printf("y: %lf, nearbyint(y): %lf\n", y, w);
float xf = 2.5;
printf("xf: %f, nearbyintf(xf): %f\n", xf, nearbyintf(xf));
long double xL = -1.3;
printf("xL: %Lf, nearbyintl(xL): %Lf\n", xL, nearbyintl(xL));
return 0;
}
注意: nearbyint()
函数对于 0.5
的情况具有特殊处理:如果要转换的数恰好与两个整数的距离相等,则按照偶数方向进行舍入(即选择更接近偶数的整数)。例如,如果要将 2.5
转换为整数,那么将近似到最接近的偶数 2
,而不是 3
。这种舍入方式称为 “银行家舍入法” 或 **“四舍六入五成双”**。
3.3 运行结果
4. nextafter,nextafterf,nextafterl
4.1 函数说明
函数声明 | 函数功能 |
---|---|
double nextafter(double x, double y); |
用于找出与给定的浮点数最接近的下一个浮点数(double) |
float nextafterf(float x, float y); |
用于找出与给定的浮点数最接近的下一个浮点数(float) |
long double nextafterl(long double x, long double y); |
用于找出与给定的浮点数最接近的下一个浮点数(long double) |
参数:
- x : 要查找其下一个浮点数的浮点数
- y : 给定浮点数的目标值,表示前进方向。
返回值:
- 如果
y
大于x
,则向正无穷方向查找; - 如果
y
小于x
,则向负无穷方向查找;如果y
等于x
,则返回y
。
4.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 1.0;
double y = 2.0;
printf("nextafter(%lf, %lf): %.20lf\n", x, y, nextafter(x, y));
printf("nextafter(%lf, %lf): %.20lf\n", y, x, nextafter(y, x));
float xf = 2.0;
float yf = 1.0;
printf("nextafterf(%f, %f): %.20f\n", xf, yf, nextafterf(xf, yf));
long double xL = -1.2;
long double yL = - 1.5;
printf("nextafterl(%Lf, %Lf): %.20Lf\n", xL, yL, nextafterl(xL, yL));
return 0;
}
注意: 由于计算机内部存储浮点数的方式是有限制的,因此在进行浮点数计算时可能会存在误差。在实际编程中,我们应该特别小心处理这些情况,避免出现异常结果。
4.3 运行结果
5. nexttoward,nexttowardf,nexttowardl
5.1 函数说明
函数声明 | 函数功能 |
---|---|
double nexttoward(double x, long double y); |
用于找出与给定的浮点数最接近、并朝着指定方向的下一个浮点数(double) |
float nexttowardf(float x, long double y); |
用于找出与给定的浮点数最接近、并朝着指定方向的下一个浮点数(float) |
long double nexttowardl(long double x, long double y); |
用于找出与给定的浮点数最接近、并朝着指定方向的下一个浮点数(long double) |
参数:
- x : 要查找其下一个浮点数的浮点数
- y : 给定浮点数的目标值,表示前进方向。
返回值:
- 如果
y
大于x
,则向正无穷方向查找; - 如果
y
小于x
,则向负无穷方向查找;如果y
等于x
,则返回y
。
5.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 1.0;
long double y = 2.0;
printf("nexttoward(%lf, %Lf): %.20lf\n", x, y, nexttoward(x, y));
float xf = 3.2;
printf("nexttowardf(%f, %Lf): %.20f\n", xf, y, nexttowardf(xf, y));
long double xL = 1.9;
printf("nexttowardl(%Lf, %Lf): %.20Lf\n", xL, y, nexttowardl(xL, y));
return 0;
}