Help us shape the future of Go! Take the 2026 Go Developer Survey

mathx

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mathx 提供常用的数学工具函数。

包含随机数生成、概率计算、浮点数精度处理等功能。

对比统计:小样本通过率的诚实区间估计与配对二分类差异检验。 显著水平对应的 z 由调用方传入(如 1.96)。收编自 agentkit/reportutil (v0.10.41 起单源在此,agentkit 侧消费本包)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp[T cmp.Ordered](v, lo, hi T) T

Clamp 将 v 限制在 [lo, hi] 范围内。 支持 int、float、string 等所有实现了 cmp.Ordered 的类型。

func DivisionN

func DivisionN(dividend, divisor float64, n int) float64

DivisionN 执行除法并保留 n 位小数(截断,非四舍五入)。

func Floor

func Floor(x float64, n int) float64

Floor 对 float64 向下取整,保留 n 位小数。

func McNemarExact

func McNemarExact(n01, n10 int) float64

McNemarExact McNemar 精确检验(双侧):n01=甲败乙过数,n10=甲过乙败数。 返回 p 值;n01+n10=0(无任何翻转)返回 1(无差异);p 钳到 1。

func Probability

func Probability(band []int) (index, r int)

Probability 根据累积概率边界返回随机选中的索引。 band 为空时返回 (0, 0)。

func ProbabilityElems

func ProbabilityElems(elems []float64, precise float64) []int

ProbabilityElems 计算概率分布的累积边界。 elems 是概率值切片(所有权重必须 > 0),precise 是精度基数。 不修改输入切片(内部排序副本)。 存在 0/负权重时直接 panic:log10(precise/0) 产生 +Inf/NaN,band 会静默 退化为全 0,Probability 恒选 index 0——概率路由静默出错比快速失败更危险。

func RandIntn

func RandIntn(maxInt int) int

RandIntn 返回 [0, maxInt) 范围内的随机整数。 并发安全,使用包级 rng 避免同纳秒碰撞。

func RangeRandFloat64

func RangeRandFloat64(min, max float64) float64

RangeRandFloat64 返回 [min, max) 范围内的随机浮点数。

func Round

func Round(x float64, n int) float64

Round 对 float64 进行四舍五入,保留 n 位小数。

func ToFixed

func ToFixed(num float64, precise int) float64

ToFixed 将 float64 格式化为指定小数位数。

func WilsonCI

func WilsonCI(passed, total int, z float64) (lo, hi float64)

WilsonCI 通过率的 Wilson 得分置信区间(小样本下比正态近似诚实)。 返回 (lo, hi);total=0 时返回 (0,0);结果钳到 [0,1]。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL