hutool.core.math

数学工具类,提供精确运算、坐标转换、位运算状态管理等功能

Module Contents

Classes

MathUtil

数学工具类,提供精确数学运算和坐标转换功能

BitStatusUtil

位运算状态工具类,使用位运算管理多状态组合

Data

API

hutool.core.math.__all__ = ('BitStatusUtil', 'MathUtil')
class hutool.core.math.MathUtil

数学工具类,提供精确数学运算和坐标转换功能

static add(value1: Union[int, float, str, decimal.Decimal], value2: Union[int, float, str, decimal.Decimal]) decimal.Decimal

精确加法,使用Decimal避免浮点精度问题

参数:
  • value1 – 加数1

  • value2 – 加数2

返回:

精确的加法结果

static point_to_radians(point: tuple) float

坐标点转弧度,根据(x, y)坐标计算对应的弧度值

参数:

point – 坐标点(x, y)

返回:

弧度值

static radians_to_point(radians: float) tuple

弧度转坐标点,根据弧度计算单位圆上的(x, y)坐标

参数:

radians – 弧度值

返回:

坐标点(x, y)

static arrangement_count(n: int, m: int) int

排列数 A(n, m) = n! / (n-m)!

参数:
  • n – 总数

  • m – 选取数

返回:

排列数

static arrangement_select(n: int, m: int) int

排列选取(同 arrangement_count)。

参数:
  • n – 总数

  • m – 选取数

返回:

排列数

static combination_count(n: int, m: int) int

组合数 C(n, m) = n! / (m! * (n-m)!)

参数:
  • n – 总数

  • m – 选取数

返回:

组合数

static combination_select(n: int, m: int) int

组合选取(同 combination_count)。

参数:
  • n – 总数

  • m – 选取数

返回:

组合数

static yuan_to_cent(yuan: float) int

元转分。

参数:

yuan – 元(如 1.23)

返回:

分(如 123)

static cent_to_yuan(cent: int) float

分转元。

参数:

cent – 分(如 123)

返回:

元(如 1.23)

class hutool.core.math.BitStatusUtil

位运算状态工具类,使用位运算管理多状态组合

static add(status: int, *values: int) int

添加状态位,将一个或多个状态值添加到当前状态中

参数:
  • status – 当前状态值

  • values – 要添加的一个或多个状态值

返回:

添加后的状态值

static has(status: int, value: int) bool

是否包含指定状态

参数:
  • status – 当前状态值

  • value – 要检测的状态值

返回:

是否包含该状态

static remove(status: int, *values: int) int

移除状态位,从当前状态中移除一个或多个状态值

参数:
  • status – 当前状态值

  • values – 要移除的一个或多个状态值

返回:

移除后的状态值

static to_binary_string(status: int) str

转二进制字符串

参数:

status – 状态值

返回:

二进制字符串表示