numf

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Helper functions for handling floats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(x float64, y float64) (max float64, min float64)

Compares and returns maximum and minimum of two floats taking NaNs into account.

func Contains added in v0.2.5

func Contains(slice []float64, s float64) bool

Checks if given float exists in the slice.

func Cumsum added in v0.2.5

func Cumsum(slice []float64) []float64

Calculates a slice of cumulative sum from given slice.

func Delta

func Delta(slice []float64) []float64

Returns the delta between all consecutive floats. Returned slice length is one item shorter.

func DropNan

func DropNan(slice []float64) []float64

Creates a new slice from given input slice without NaNs

func DropNanInplace added in v0.23.1

func DropNanInplace(slice *[]float64)

Drops NaNs from a slice in place.

func FillNan

func FillNan(slice []float64, method string, prefill bool, validTime int) []float64

Fills NaN values with a value based on given method:

"previous" // fills the NaNs with previous value
"linear"   // fills the NaNs with linear interpolation

Filling starts from first valid value, thus leaving any preceding NaNs untouched. By setting prefill = true, first valid value is used to replace also the preceding NaNs.

One sample's lifetime can be set by setting validTime value to > 0. Filling is then performed for validTime samples. Using validTime overrides prefill = true -setting

func FillNanInplace added in v0.23.3

func FillNanInplace(slice *[]float64, method string, prefill bool, validTime int)

Fills NaN values inplace with a value based on given method:

"previous" // fills the NaNs with previous value
"linear"   // fills the NaNs with linear interpolation

Filling starts from first valid value, thus leaving any preceding NaNs untouched. By setting prefill = true, first valid value is used to replace also the preceding NaNs.

One sample's lifetime can be set by setting validTime value to > 0. Filling is then performed for validTime samples. Using validTime overrides prefill = true -setting

func FindIndex

func FindIndex(slice []float64, val float64) (int, bool)

Finds the index of first occurrence of the given value.

func GetIndex added in v0.24.0

func GetIndex(slice []float64, val float64) int

Finds the index of first occurrence of the given value and returns the index. If not found index = -1.

func Insert added in v0.2.3

func Insert(slice []float64, idx int, val float64) []float64

Inserts given value to given index into a slice.

func IsEqualSlice added in v0.21.0

func IsEqualSlice(s1, s2 []float64) bool

Checks if two slices are equal, element-wise. NaN and Inf are also handled.

func IsSameSign added in v0.21.0

func IsSameSign(x float64, y float64) bool

Checks if two values have same sign. Zero sign is ambiguous and is considered to be both positive and negative.

func IsValid

func IsValid(value float64) bool

Checks whether the given float is a valid number instead of NaN or Inf.

func Max added in v0.27.0

func Max(a, b float64) float64

Faster Max

func Median added in v0.26.0

func Median(data []float64) float64

Finds the middle value or mean of middle values from dataset using Quickselect Note: Contents of data is altered

func MedianFromSorted added in v0.26.4

func MedianFromSorted(data []float64) float64

Median from sorted data

func Min added in v0.27.0

func Min(a, b float64) float64

Faster Min

func MulSlices added in v0.16.0

func MulSlices(s1, s2 []float64) []float64

Multiplies two slices of same length element-wise.

func NanSlice

func NanSlice(size int) (nanslice []float64)

Creates a slice of NaNs of given size.

func NumRange

func NumRange(start int, end int, step int) (numberrange []float64)

Returns an evenly spaced slice of floats, between <start> and <end> with spacing <step>.

func PrettyPerc added in v0.25.6

func PrettyPerc(v1, v2 float64, decimals int) float64

Calculate ratio v1/v2 as percentage value with defined amount of decimals

func QuickSelect added in v0.26.0

func QuickSelect(data []float64, k int) float64

Finds kth largest value from data. NOTE: Will crash if len(data)=0

func QuickSelects added in v0.26.3

func QuickSelects(data []float64, k int) (float64, float64)

Finds kth and (k+1) th largest value from data. NOTE: Will crash if len(data)=0

func Quicksort added in v0.26.0

func Quicksort(data []float64)

Sorts data into ascending order using Quicksort. Note: Not safe to use with NaNs. If data is already sorted, the algorithm will be very slow compared to regular sort.

func RemoveFrom added in v0.2.3

func RemoveFrom(slice []float64, s int) []float64

Removes an integer from given index

func ReplaceNan

func ReplaceNan(value float64, replacewith float64) float64

Replaces given value with a new one, if given value is NaN or Inf.

func ReplaceNans

func ReplaceNans(values []float64, replacewith float64) []float64

Replaces all NaNs in a slice with given value and returns a new slice.

func ReplaceNansInplace added in v0.23.3

func ReplaceNansInplace(values *[]float64, replacewith float64)

Replaces all NaNs in a slice in place with given value.

func RoundTo

func RoundTo(val float64, digits int) float64

Rounds the number to given significant digits.

func RoundToNearest added in v0.2.4

func RoundToNearest(value float64, nearest float64) float64

Rounds <value> to <nearest> value

func Select added in v0.21.1

func Select(condition bool, i1, i2 float64) float64

Returns i1 if condition == TRUE, else returns i2

func SetDefault

func SetDefault(defaultval float64, confval float64) float64

Replaces default value for a parameter, if the configuration value is zero (ie. initialized empty variable). Note that the function does not make any difference, if the configuration is set to zero on purpose.

func SliceOf added in v0.2.5

func SliceOf(value float64, size int) []float64

Creates a slice of given size filled with given value.

func SwitchIf added in v0.16.0

func SwitchIf(condition bool, i1, i2 float64) (float64, float64)

Returns the given values without changes, if condition == FALSE. Returns the values in switched order, if condition == TRUE.

Types

type Rounded

type Rounded struct {
	Rawvalue       float64 // raw value with given digits but no prefix
	Prefix         string  // prefix string
	Value          float64 // rounded value with given digits and prefix
	Response       string  // response string as <Value> <Prefix><unit>
	Prettyvalue    float64 // rounded value with one decimal
	Prettyresponse string  // response string as <Value> <Prefix><unit> ; if value is <0.1 , shows "less than 0.1"
}

func RoundWithPrefix

func RoundWithPrefix(v float64, digits int, unit string, prefix string) Rounded

Returns the rounded value to given digits and correct prefix (M for Megas, k for Kilos etc.) Special case is abs(value) between 1000....10000, which is not converted to kilos (because it looks nicer) set prefix to force certain prefix, otherwise the function figures it out on its' own. These units are excluded from having a prefix

noprefixUnits := []string{"%", "cycles", "years", "°c", "°lon", "°lat", "events", "", " "}

Jump to

Keyboard shortcuts

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