---
title: nexttoward
description: "计算x在y方向的下个浮点数。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2300/accel/kunpengaccel_kml_16_0568.html
sourcePath: /source/zh/kunpengboostkithistory/2300/accel/kunpengaccel_kml_16_0568.html
indexId: b4d594fae913ec3e24e444876343dadea199d53ae83625bebccc8b9e46484a4574
---
# nexttoward

计算x在y方向的下个浮点数。

#### 接口定义

C interface：

float nexttowardf(float x, long double y);

double nexttoward(double x, long double y);


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| x | 在nexttowardf中，x是单精度浮点类型。 在nexttoward中，x是双精度浮点类型。 | 表示输入数据的浮点值。 | 输入 |
| y | 在nexttowardf中，y是long double浮点类型。 在nexttoward中，y是long double浮点类型。 | 表示输入数据的浮点值。 | 输入 |


#### 返回值

返回x的在y方向上的下一个浮点值。

- 输入x=y，返回x。
- 输入x或者y等于nan，返回nan。


#### 依赖

C: "km.h"


#### 示例

C interface：
```
// typical usage
double x1 = 1.0, x2 = 1.0;
double y1 = 1.0, y2 = 1.1;
double z1 = 1.0, z2 = 0.9;
// special handling
double e = INFINITY, f = -INFINITY, g = NAN;
// print result
printf("\t/*\n");
printf("\t * nexttoward(1.0, 1.0) = %a\n", nexttoward(x1, (long double)y1));
printf("\t * nexttoward(1.0, 1.1) = %a\n", nexttoward(y1, (long double)y2));
printf("\t * nexttoward(1.0, 0.9) = %a\n", nexttoward(z1, (long double)z2));
printf("\t * nexttoward(INFINITY, 0) = %a\n", nexttoward(e, 0.0));
printf("\t * nexttoward(-INFINITY, 1.0) = %a\n", nexttoward(f, x1));
printf("\t * nexttoward(NAN, 1.0) = %a\n", nexttoward(g, x1));
printf("\t * nexttoward(NAN, INFINITY) = %a\n", nexttoward(g, e));
printf("\t */\n");
return 0;
/*
* nexttoward(1.0, 1.0) = 0x1p+0
* nexttoward(1.0, 1.1) = 0x1.0000000000001p+0
* nexttoward(1.0, 0.9) = 0x1.fffffffffffffp-1
* nexttoward(INFINITY, 0) = 0x1.fffffffffffffp+1023
* nexttoward(-INFINITY, 1.0) = -0x1.fffffffffffffp+1023
* nexttoward(NAN, 1.0) = nan
* nexttoward(NAN, INFINITY) = nan
*/
```
