---
title: FindNearestOne
description: "查找表中最接近指定值的元素。查找到的元素及其索引分别存储在outVal和outIndex中。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2300/accel/kunpengaccel_hmpp_06_0153.html
sourcePath: /source/zh/kunpengboostkithistory/2300/accel/kunpengaccel_hmpp_06_0153.html
indexId: 42566354da5aa4d0009a7c9e43e7e97675d088f00006d8ff5fec1203b757df9a75
---
# FindNearestOne

查找表中最接近指定值的元素。查找到的元素及其索引分别存储在outVal和outIndex中。

其中表中元素必须满足条件：table[n] ≤table[n+1]。最接近指的是：min(|inVal -table[n]|)。

函数接口声明如下：

HmppResult HMPPS_FindNearestOne_16u(uint16_t inVal, uint16_t *outVal, int32_t *outIndex, const uint16_t *table, int32_t tblLen);

#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| inVal | 指定元素。 | (0, UINT16\_MAX] | 输入 |
| outVal | 查找结果元素的值。 | (0, UINT16\_MAX] | 输出 |
| outIndex | 查找结果元素的索引。 | [0,tblLen\-1] | 输出 |
| table | 指向单调不减向量表。 | 非空 | 输入 |
| tblLen | 向量表的长度。 | (0, INT\_MAX] | 输入 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | table为空指针。 |
| HMPP\_STS\_SIZE\_ERR | tblLen小于或等于0。 |


#### 示例

```
#define  BUFFER_SIZE_T 11
void FindNearestOneExample()
{
uint16_t table[BUFFER_SIZE_T] = {32, 545, 766, 876, 1222, 1334, 1687, 3452, 4556, 32452, 45422};
uint16_t inVal = 4559;
uint16_t outVal;
int32_t outIndex;
HmppResult result = HMPPS_FindNearestOne_16u(inVal, &outVal, &outIndex, table, BUFFER_SIZE_T );
if (result == HMPP_STS_NO_ERR) {
printf("outVal = %d\n", outVal);
printf("outIndex = %d\n", outIndex);
}
}
```

运行结果：

```
outVal = 4556
outIndex = 8
```
