package scalar

import "gonum.org/v1/gonum/floats/scalar"

Package scalar provides a set of helper routines for dealing with float64 values.

Index

Examples

Functions

func EqualWithinAbs

func EqualWithinAbs(a, b, tol float64) bool

EqualWithinAbs returns true when a and b have an absolute difference not greater than tol.

func EqualWithinAbsOrRel

func EqualWithinAbsOrRel(a, b, absTol, relTol float64) bool

EqualWithinAbsOrRel returns true when a and b are equal to within the absolute or relative tolerances. See EqualWithinAbs and EqualWithinRel for details.

func EqualWithinRel

func EqualWithinRel(a, b, tol float64) bool

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

func EqualWithinULP(a, b float64, ulp uint) bool

EqualWithinULP returns true when a and b are equal to within the specified number of floating point units in the last place.

func NaNPayload

func NaNPayload(f float64) (payload uint64, ok bool)

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

func NaNWith(payload uint64) float64

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

func ParseWithNA(s, missing string) (value, weight float64, err error)

ParseWithNA converts the string s to a float64 in value. If s equals missing, weight is returned as 0, otherwise 1.

Example

Code:play 

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

func Round(x float64, prec int) float64

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

func RoundEven

func RoundEven(x float64, prec int) float64

RoundEven returns the half even rounded value of x with prec precision.

Special cases are:

RoundEven(±0) = +0
RoundEven(±Inf) = ±Inf
RoundEven(NaN) = NaN

func Same

func Same(a, b float64) bool

Same returns true when the inputs have the same value, allowing NaN equality.

Source Files

doc.go scalar.go

Version
v0.14.0 (latest)
Published
Aug 19, 2023
Platform
linux/amd64
Imports
2 packages
Last checked
5 hours ago

Tools for package owners.