emath

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultAbsTol = 0.0

DefaultAbsTol is zero.

View Source
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.
`

Types

type FloatTol

type FloatTol struct {
	// Relative and Absolute Tolerance
	Rel, Abs float64
}

FloatTol represents the acceptable tolerance for float equality.

Jump to

Keyboard shortcuts

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