Documentation
¶
Overview ¶
Package mathx contains math utilities. It means to be a complement to the standard math package.
Index ¶
Examples ¶
Constants ¶
View Source
const ( MaxUint = ^uint(0) MinUint = 0 MaxInt = int(MaxUint >> 1) MinInt = -MaxInt - 1 )
Compile time constants to determine the min and max values of the int and uint types.
Variables ¶
This section is empty.
Functions ¶
func Near ¶
Near reports if 2 float64 numbers are "near" to each other. The caller is responsible to provide a sensible epsilon.
"near" is defined as the following:
near := math.Abs(a - b) < eps
Corner cases:
- if a==b, result is true (eps will not be checked, may be NaN)
- Inf is near to Inf (even if eps=NaN; consequence of 1.)
- -Inf is near to -Inf (even if eps=NaN; consequence of 1.)
- NaN is not near to anything (not even to NaN)
- eps=Inf results in true (unless any of a or b is NaN)
func Round ¶
Round returns x rounded to the given unit. Tip: x is "arbitrary", maybe greater than 1. For example:
Round(0.363636, 0.001) // 0.364 Round(0.363636, 0.01) // 0.36 Round(0.363636, 0.1) // 0.4 Round(0.363636, 0.05) // 0.35 Round(3.2, 1) // 3 Round(32, 5) // 30 Round(33, 5) // 35 Round(32, 10) // 30
For details, see https://stackoverflow.com/a/39544897/1705598
Example ¶
fmt.Printf("%.4f\n", Round(0.363636, 0.001)) // 0.364 fmt.Printf("%.4f\n", Round(0.363636, 0.01)) // 0.36 fmt.Printf("%.4f\n", Round(0.363636, 0.1)) // 0.4 fmt.Printf("%.4f\n", Round(0.363636, 0.05)) // 0.35 fmt.Printf("%.4f\n", Round(3.2, 1)) // 3 fmt.Printf("%.4f\n", Round(32, 5)) // 30 fmt.Printf("%.4f\n", Round(33, 5)) // 35 fmt.Printf("%.4f\n", Round(32, 10)) // 30 fmt.Printf("%.4f\n", Round(-0.363636, 0.001)) // -0.364 fmt.Printf("%.4f\n", Round(-0.363636, 0.01)) // -0.36 fmt.Printf("%.4f\n", Round(-0.363636, 0.1)) // -0.4 fmt.Printf("%.4f\n", Round(-0.363636, 0.05)) // -0.35 fmt.Printf("%.4f\n", Round(-3.2, 1)) // -3 fmt.Printf("%.4f\n", Round(-32, 5)) // -30 fmt.Printf("%.4f\n", Round(-33, 5)) // -35 fmt.Printf("%.4f\n", Round(-32, 10)) // -30
Output: 0.3640 0.3600 0.4000 0.3500 3.0000 30.0000 35.0000 30.0000 -0.3640 -0.3600 -0.4000 -0.3500 -3.0000 -30.0000 -35.0000 -30.0000
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.