Documentation ¶
Index ¶
- Constants
- func Clamp[T constraints.Ordered](n, min, max T) T
- func Close[T constraints.Float](a, b T) bool
- func Max[T constraints.Ordered](a, b T) T
- func Min[T constraints.Ordered](a, b T) T
- func Pow[T constraints.Integer](x T, n uint32) T
- func Sign[N constraints.Signed](n N) N
- func WithinTol[T constraints.Float](a, b T, tol FloatTol) bool
- type FloatTol
Constants ¶
const DefaultAbsTol = 0.0
DefaultAbsTol is zero.
const DefaultRelTol = 1e-9
DefaultRelTol is the default relative tolerance for Close().
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
func Clamp[T constraints.Ordered](n, min, max T) T
Clamp the value to be within the closed interval [min, max].
fmt.Println(Clamp[int](-5, 0.0, 5)) >> 0 fmt.Println(Clamp[f64](6.0, 0.0, 5.0))) >> 5.0
func Close ¶
func Close[T constraints.Float](a, b T) bool
Close checks whether two floats are appromixately equal within 1e-9 relative tolerance (about 9 decimal places.) Identical to WithinTol(a, b, FloatTol{Rel: DefaultRelTol})
func Max ¶
func Max[T constraints.Ordered](a, b T) T
Max of a and b.
fmt.Println(2 == Max(int64(2), int64(-1))) //true
func Min ¶
func Min[T constraints.Ordered](a, b T) T
Min of a and b.
fmt.Println(-1 == Min(int64(2), int64(-1))) //true
func Pow ¶
func Pow[T constraints.Integer](x T, n uint32) T
Pow raises x to the nth power via exponentiation-by-squaring.
fmt.Println(Pow(2, 3)) 8
func Sign ¶
func Sign[N constraints.Signed](n N) N
Sign returns the sign: -1 if n < 0, 1 if n > 0, 0 otherwise.
func WithinTol ¶
func WithinTol[T constraints.Float](a, b T, tol FloatTol) bool
WithinTol checks whether two floating point numbers are approximately equal. The logic is taken from https://docs.python.org/3/library/math.html#math.isclose, the documentation of which I reproduce here.
` Whether or not two values are considered close is determined according to given absolute and relative tolerances. rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than zero. abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol must be at least zero. If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol). The IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. Specifically, NaN is not considered close to any other value, including NaN. inf and -inf are only considered close to themselves. `