Documentation ¶
Overview ¶
Package scalar provides a set of helper routines for dealing with float64 values.
Index ¶
- func EqualWithinAbs(a, b, tol float64) bool
- func EqualWithinAbsOrRel(a, b, absTol, relTol float64) bool
- func EqualWithinRel(a, b, tol float64) bool
- func EqualWithinULP(a, b float64, ulp uint) bool
- func NaNPayload(f float64) (payload uint64, ok bool)
- func NaNWith(payload uint64) float64
- func ParseWithNA(s, missing string) (value, weight float64, err error)
- func Round(x float64, prec int) float64
- func RoundEven(x float64, prec int) float64
- func Same(a, b float64) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EqualWithinAbs ¶
EqualWithinAbs returns true when a and b have an absolute difference not greater than tol.
func EqualWithinAbsOrRel ¶
EqualWithinAbsOrRel returns true when a and b are equal to within the absolute or relative tolerances. See EqualWithinAbs and EqualWithinRel for details.
func EqualWithinRel ¶
EqualWithinRel returns true when the difference between a and b is not greater than tol times the greater absolute value of a and b,
abs(a-b) <= tol * max(abs(a), abs(b)).
func EqualWithinULP ¶
EqualWithinULP returns true when a and b are equal to within the specified number of floating point units in the last place.
func NaNPayload ¶
NaNPayload returns the lowest 51 bits payload of an IEEE 754 "quiet not-a-number". For values of f other than quiet-NaN, NaNPayload returns zero and false.
func NaNWith ¶
NaNWith returns an IEEE 754 "quiet not-a-number" value with the payload specified in the low 51 bits of payload. The NaN returned by math.NaN has a bit pattern equal to NaNWith(1).
func ParseWithNA ¶
ParseWithNA converts the string s to a float64 in value. If s equals missing, weight is returned as 0, otherwise 1.
Example ¶
package main import ( "bufio" "fmt" "log" "strings" "gonum.org/v1/gonum/floats/scalar" "gonum.org/v1/gonum/stat" ) func main() { // Calculate the mean of a list of numbers // ignoring missing values. const data = `6 missing 4 ` var vals, weights []float64 sc := bufio.NewScanner(strings.NewReader(data)) for sc.Scan() { v, w, err := scalar.ParseWithNA(sc.Text(), "missing") if err != nil { log.Fatal(err) } vals = append(vals, v) weights = append(weights, w) } err := sc.Err() if err != nil { log.Fatal(err) } fmt.Println(stat.Mean(vals, weights)) }
Output: 5
func Round ¶
Round returns the half away from zero rounded value of x with prec precision.
Special cases are:
Round(±0) = +0 Round(±Inf) = ±Inf Round(NaN) = NaN
Types ¶
This section is empty.