scalar

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: BSD-3-Clause Imports: 2 Imported by: 38

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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
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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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