Documentation ¶
Overview ¶
Helper functions for handling floats.
Index ¶
- func Compare(x float64, y float64) (max float64, min float64)
- func Contains(slice []float64, s float64) bool
- func Cumsum(slice []float64) []float64
- func Delta(slice []float64) []float64
- func DropNan(slice []float64) []float64
- func DropNanInplace(slice *[]float64)
- func FillNan(slice []float64, method string, prefill bool, validTime int) []float64
- func FillNanInplace(slice *[]float64, method string, prefill bool, validTime int)
- func FindIndex(slice []float64, val float64) (int, bool)
- func GetIndex(slice []float64, val float64) int
- func Insert(slice []float64, idx int, val float64) []float64
- func IsEqualSlice(s1, s2 []float64) bool
- func IsSameSign(x float64, y float64) bool
- func IsValid(value float64) bool
- func Max(a, b float64) float64
- func Median(data []float64) float64
- func MedianFromSorted(data []float64) float64
- func Min(a, b float64) float64
- func MulSlices(s1, s2 []float64) []float64
- func NanSlice(size int) (nanslice []float64)
- func NumRange(start int, end int, step int) (numberrange []float64)
- func PrettyPerc(v1, v2 float64, decimals int) float64
- func QuickSelect(data []float64, k int) float64
- func QuickSelects(data []float64, k int) (float64, float64)
- func Quicksort(data []float64)
- func RemoveFrom(slice []float64, s int) []float64
- func ReplaceNan(value float64, replacewith float64) float64
- func ReplaceNans(values []float64, replacewith float64) []float64
- func ReplaceNansInplace(values *[]float64, replacewith float64)
- func RoundTo(val float64, digits int) float64
- func RoundToNearest(value float64, nearest float64) float64
- func Select(condition bool, i1, i2 float64) float64
- func SetDefault(defaultval float64, confval float64) float64
- func SliceOf(value float64, size int) []float64
- func SwitchIf(condition bool, i1, i2 float64) (float64, float64)
- type Rounded
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delta ¶
Returns the delta between all consecutive floats. Returned slice length is one item shorter.
func DropNanInplace ¶ added in v0.23.1
func DropNanInplace(slice *[]float64)
Drops NaNs from a slice in place.
func FillNan ¶
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
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 GetIndex ¶ added in v0.24.0
Finds the index of first occurrence of the given value and returns the index. If not found index = -1.
func IsEqualSlice ¶ added in v0.21.0
Checks if two slices are equal, element-wise. NaN and Inf are also handled.
func IsSameSign ¶ added in v0.21.0
Checks if two values have same sign. Zero sign is ambiguous and is considered to be both positive and negative.
func Median ¶ added in v0.26.0
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
Median from sorted data
func NumRange ¶
Returns an evenly spaced slice of floats, between <start> and <end> with spacing <step>.
func PrettyPerc ¶ added in v0.25.6
Calculate ratio v1/v2 as percentage value with defined amount of decimals
func QuickSelect ¶ added in v0.26.0
Finds kth largest value from data. NOTE: Will crash if len(data)=0
func QuickSelects ¶ added in v0.26.3
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
Removes an integer from given index
func ReplaceNan ¶
Replaces given value with a new one, if given value is NaN or Inf.
func ReplaceNans ¶
Replaces all NaNs in a slice with given value and returns a new slice.
func ReplaceNansInplace ¶ added in v0.23.3
Replaces all NaNs in a slice in place with given value.
func RoundToNearest ¶ added in v0.2.4
Rounds <value> to <nearest> value
func SetDefault ¶
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.
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 ¶
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", "", " "}