---
title: fmod
description: "返回按元素划分的除法余数。注意：这是C库函数fmod的NumPy实现，返回的余数与x1具有相同符号，等效于Matlab的rem函数，不应与Python的模数运算x1 % x2混淆。"
url: https://www.hikunpeng.com/document/detail/zh/hpchistory/hpckit/develop2400/kunpengaccel_kml_0998.html
sourcePath: /source/zh/hpchistory/hpckit/develop2400/kunpengaccel_kml_0998.html
indexId: e710808ba928c55d94a0aed9d461af1f93fa2378a97af4dbfb248590c74f8bca67
---
# fmod

#### 功能描述

返回按元素划分的除法余数。注意：这是C库函数fmod的NumPy实现，返回的余数与x1具有相同符号，等效于Matlab的rem函数，不应与Python的模数运算x1 % x2混淆。


#### 必选输入参数

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| x1 | array\_like | 被除数，输入的数组或标量。 |
| x2 | array\_like | 除数，输入的数组或标量。若x1.shape!=x2.shape，则它们必须能广播至相同形状。 |


#### 可选输入参数

| 参数名 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| out | ndarray/ndarray的元组 | None | 计算结果保存的位置。如果提供，其形状必须与输入数组广播后的形状一致。未指定时，返回一个新的数组。 |
| where | array\_like | None | 将被广播至输入的条件数组。在条件为真处的元素，其计算结果会存入out中，其余位置保留原有值。 |
| \*\*kwargs | \- | \- | 其他关键字参数，请参见NumPy官方文档Universal functions (ufunc)(https://numpy.org/doc/stable/reference/ufuncs.html\#ufuncs\-kwargs)。 |


#### 返回数据

| 类型 | 说明 |
| --- | --- |
| ndarray/scalar | x1 % x2的结果，结果符号与x1保持一致。 |


#### 示例

```
>>> import numpy as np
>>> np.fmod(-3,2)
-1
>>>
>>> x1 = [-1, 2, -3, 4, -5]
>>> np.fmod(x1, 2)
array([-1,  0, -1,  0, -1])
>>> 
>>> x2 = [1, -1, 2, -2, 3]
>>> np.fmod(x1, x2)
array([ 0,  0, -1,  0, -2])
>>>
```
