import{_ as s,c as i,o as a,a2 as n}from"./chunks/framework.BV61Qrc0.js";const o=JSON.parse('{"title":"mbcp.mp_math.utils","description":"","frontmatter":{"title":"mbcp.mp_math.utils"},"headers":[],"relativePath":"en/api/mp_math/utils.md","filePath":"en/api/mp_math/utils.md"}'),l={name:"en/api/mp_math/utils.md"},h=n(`

def clamp()

区间截断函数。

Args:

x:

min_:

max_:

Source code
python
def clamp(x: float, min_: float, max_: float) -> float:
    """
    区间截断函数。
    Args:
        x:
        min_:
        max_:

    Returns:
        限制后的值
    """
    return max(min(x, max_), min_)

def approx(x: float = 0.0, y: float = APPROX)

判断两个数是否近似相等。或包装一个实数,用于判断是否近似于0。

Args:

x:

y:

epsilon:

Source code
python
def approx(x: float, y: float=0.0, epsilon: float=APPROX) -> bool:
    """
    判断两个数是否近似相等。或包装一个实数,用于判断是否近似于0。
    Args:
        x:
        y:
        epsilon:

    Returns:
        是否近似相等
    """
    return abs(x - y) < epsilon

def sign(x: float = False)

获取数的符号。

Args:

x: 数

only_neg: 是否只返回负数的符号

Source code
python
def sign(x: float, only_neg: bool=False) -> str:
    """获取数的符号。
    Args:
        x: 数
        only_neg: 是否只返回负数的符号
    Returns:
        符号 + - ""
    """
    if x > 0:
        return '+' if not only_neg else ''
    elif x < 0:
        return '-'
    else:
        return ''

def sign_format(x: float = False)

格式化符号数 -1 -> -1 1 -> +1 0 -> ""

Args:

x: 数

only_neg: 是否只返回负数的符号

Source code
python
def sign_format(x: float, only_neg: bool=False) -> str:
    """格式化符号数
    -1 -> -1
    1 -> +1
    0 -> ""
    Args:
        x: 数
        only_neg: 是否只返回负数的符号
    Returns:
        符号 + - ""
    """
    if x > 0:
        return f'+{x}' if not only_neg else f'{x}'
    elif x < 0:
        return f'-{abs(x)}'
    else:
        return ''

class Approx

Source code
python
def __init__(self, value: RealNumber):
    self.value = value
Source code
python
def __eq__(self, other):
    if isinstance(self.value, (float, int)):
        if isinstance(other, (float, int)):
            return abs(self.value - other) < APPROX
        else:
            self.raise_type_error(other)
    elif isinstance(self.value, Vector3):
        if isinstance(other, (Vector3, Point3, Plane3, Line3)):
            return all([approx(self.value.x, other.x), approx(self.value.y, other.y), approx(self.value.z, other.z)])
        else:
            self.raise_type_error(other)
Source code
python
def raise_type_error(self, other):
    raise TypeError(f'Unsupported type: {type(self.value)} and {type(other)}')
Source code
python
def __ne__(self, other):
    return not self.__eq__(other)
`,35),t=[h];function e(p,k,r,d,F,E){return a(),i("div",null,t)}const y=s(l,[["render",e]]);export{o as __pageData,y as default};