// Package mathutil provide math(int, number) util functions. eg: convert, math calc, random
package mathutil import ( ) // Mul computes the `a*b` value, rounding the result. func [, comdef.Number]( , ) float64 { return math.Round(SafeFloat() * SafeFloat()) } // MulF2i computes the float64 type a * b value, rounding the result to an integer. func (, float64) int { return int(math.Round( * )) } // Div computes the `a/b` value, result uses a round handle. func [, comdef.Number]( , ) float64 { return math.Round(SafeFloat() / SafeFloat()) } // DivInt computes the int type a / b value, rounding the result to an integer. func [ comdef.Integer](, ) int { := math.Round(float64() / float64()) return int() } // DivF2i computes the float64 type a / b value, rounding the result to an integer. func (, float64) int { return int(math.Round( / )) } // Percent returns a value percentage of the total. eg: 1/100 = 1.0% func (, int) float64 { if == 0 { return float64(0) } return (float64() / float64()) * 100 } // Range a number range expression, and handle each value. eg: "1-100,123,124" func ( string, func( int)) error { for , := range strings.Split(, ",") { = strings.TrimSpace() if == "" { continue } // is range, eg: "1-100", "-20-2" if := checkfn.IndexByteAfter(, '-', 1); > 0 { , , := parseIntRange(, ) if != nil { return } // range number for := ; <= ; ++ { () } } else { , := strconv.Atoi() if != nil { return fmt.Errorf("invalid integer value: %q", ) } () } } return nil } // Expand a number range expression to int[]. eg: "1-100,123,124" func ( string) ([]int, error) { var []int for , := range strings.Split(, ",") { if == "" { continue } // is range, eg: "1-100", "-20-2" if := checkfn.IndexByteAfter(, '-', 1); > 0 { , := expandIntRange(, ) if != nil { return nil, } = append(, ...) } else { , := strconv.Atoi() if != nil { return nil, fmt.Errorf("invalid integer value: %q", ) } = append(, ) } } return , nil } // 处理范围格式 // eg: "1-30" -> [1, 30], "-20-2" -> [-20, 2] func parseIntRange( string, int) ( int, int, error) { , := [:], [+1:] , = strconv.Atoi() if != nil { = fmt.Errorf("invalid range start value: %q", ) return } , = strconv.Atoi() if != nil { = fmt.Errorf("invalid range end value: %q", ) return } // swap min and max if > { , = , } return } // 将 "1-30" 转换为 int 列表 func expandIntRange( string, int) ([]int, error) { var []int , , := parseIntRange(, ) if != nil { return nil, } for := ; <= ; ++ { = append(, ) } return , nil }