go-math: github.com/spatialcurrent/go-math/pkg/math Index | Examples | Files

package math

import "github.com/spatialcurrent/go-math/pkg/math"

Package math provides math functions that support varied types, but tries to preserve the original type if possible. For example, you can sum a slice of ints and floats.

Index

Examples

Package Files

Add.go Compare.go Divide.go ErrInvalidAddition.go ErrInvalidComparison.go ErrInvalidDivision.go ErrInvalidKind.go ErrInvalidMultiplication.go ErrInvalidPower.go ErrInvalidSubtraction.go Max.go Mean.go Min.go Multiply.go Pow.go Product.go Subtract.go Sum.go math.go

Variables

var (
    ErrEmptyInput = errors.New("empty input")
)

func Add Uses

func Add(a interface{}, b interface{}) (interface{}, error)

Add adds 2 values together while following a few simple rules to convert integers and floats. Supports types uint8, int32, int64, int, float64, and time.Duration. Returns an error if the values cannot be added.

Code:

out, err := Add(time.Hour*1, time.Hour*2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3h0m0s

Code:

out, err := Add(1.11, 2.22)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3.33

Code:

out, err := Add(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3

Code:

out, err := Add(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3

Code:

out, err := Add(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3

Code:

out, err := Add(uint8(1), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3

func Compare Uses

func Compare(a interface{}, b interface{}) (int, error)

Compare compares 2 values and follows a few simple rules to compare integers and floats. Supports types uint8, int32, int64, int, float64, and time.Time. Returns an error if not comparable. Return value of -1 indicates a is less than b. Return value of 1 indicates a is greater than b. Return value of 0 indicates a is equivalent to b by value.

Code:

out, err := Compare(time.Hour*1, time.Hour*2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Compare(1.11, 2.22)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Compare(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Compare(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Compare(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

now := time.Now()
out, err := Compare(now, now.Add(time.Hour*1))
if err != nil {
    panic(err)
}
fmt.Println(out)

Code:

out, err := Compare(uint8(1), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

func Divide Uses

func Divide(a interface{}, b interface{}) (out interface{}, err error)

Divide divides a by b while following a few simple rules to maximize precision. Rather than truncating integers, this function converts the input to floats if a is not divisible by b.

- If integer a is divisible by integer b, then returns an integer.
- If integer a is not divisible by integer b, then converts both numbers to floats and returns the remainder.

Supports types uint8, int32, int64, int, float64, and time.Duration. Catches divide by zero panics and returns as errors. Returns an error if a cannot be divided by b.

Code:

out, err := Divide(time.Hour*4, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2h0m0s

Code:

out, err := Divide(time.Hour*3, time.Hour*2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1.5

Code:

out, err := Divide(1.11, 2.22)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

0.5

Code:

out, err := Divide(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

0.5

Code:

out, err := Divide(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

0.5

Code:

out, err := Divide(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

0.5

Code:

out, err := Divide(uint8(4), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

func Max Uses

func Max(in interface{}) (interface{}, error)

Max returns the maximum value from the array or slice of durations, floats, ints, and times.

Code:

out, err := Max([]time.Duration{
    time.Hour * 2,
    time.Hour * 3,
    time.Hour * 1,
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3h0m0s

Code:

out, err := Max([]float64{1.11, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2.22

Code:

out, err := Max([]interface{}{10, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

10

Code:

out, err := Max([]int{10, 20})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

20

Code:

out, err := Max([]time.Time{
    time.Now(),
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Code:

out, err := Max([]uint8{1, 2, 3})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3

func Mean Uses

func Mean(in interface{}) (interface{}, error)

Mean returns the mean value from the array or slice of ints, floats, or durations. For durations, returns the average duration. For ints and floats, returns a float64.

Code:

out, err := Mean([]time.Duration{
    time.Hour * 2,
    time.Hour * 1,
    time.Hour * 3,
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2h0m0s

Code:

out, err := Mean([]float64{1.11, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1.665

Code:

out, err := Mean([]interface{}{10, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

6.11

Code:

out, err := Mean([]int{10, 20})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

15

Code:

out, err := Mean([]uint8{1, 2, 3})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

func Min Uses

func Min(in interface{}) (interface{}, error)

Min returns the minimum value from the array or slice of durations, floats, ints, and times.

Code:

out, err := Min([]time.Duration{
    time.Hour * 2,
    time.Hour * 3,
    time.Hour * 1,
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1h0m0s

Code:

out, err := Min([]float64{1.11, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1.11

Code:

out, err := Min([]interface{}{10, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2.22

Code:

out, err := Min([]int{10, 20})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

10

Code:

out, err := Min([]time.Time{
    time.Now(),
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Code:

out, err := Min([]uint8{1, 2, 3})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1

func Multiply Uses

func Multiply(a interface{}, b interface{}) (interface{}, error)

Multiply multiplies 2 values together while following a few simple rules to convert integers and floats. Supports types uint8, int32, int64, int, float64, and time.Duration. Returns an error if the values cannot be multiplied.

Code:

out, err := Multiply(0.5, time.Hour*3)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1h30m0s

Code:

out, err := Multiply(2, time.Hour*1)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2h0m0s

Code:

out, err := Multiply(0.5, 3.0)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1.5

Code:

out, err := Multiply(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

Code:

out, err := Multiply(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

Code:

out, err := Multiply(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

Code:

out, err := Multiply(uint8(1), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

func Pow Uses

func Pow(a interface{}, b interface{}) (out interface{}, err error)

Pow raises a to the power of b (a^b). If a and b are both (unsigned-)integers, then returns an int. Otherwise, returns a float64. Supports types uint8, int32, int64, int, and float64.

Code:

out, err := Pow(1.11, 2.22)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1.2607152693942678

Code:

out, err := Pow(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1

Code:

out, err := Pow(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1

Code:

out, err := Pow(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1

Code:

out, err := Pow(uint8(4), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

16

func Product Uses

func Product(in interface{}) (interface{}, error)

Product returns the total product (multiplied value) from the array or slice of ints, floats, or durations.

Code:

out, err := Product([]interface{}{
    time.Hour * 1,
    2,
    4,
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

8h0m0s

Code:

out, err := Product([]float64{1.5, 3.0})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

4.5

Code:

out, err := Product([]interface{}{10, 2.5})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

25

Code:

out, err := Product([]int{10, 20})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

200

Code:

out, err := Product([]uint8{1, 2, 3})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

6

func Subtract Uses

func Subtract(a interface{}, b interface{}) (interface{}, error)

Subtract subtracts the second value from the first value while following a few simple rules to convert integers and floats. Supports types uint8, int32, int64, int, float64, time.Time, and time.Duration. Returns an error if the values cannot be added.

Code:

out, err := Subtract(time.Hour*3, time.Hour*2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1h0m0s

Code:

out, err := Subtract(1.11, 2.22)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1.11

Code:

out, err := Subtract(int32(1), int32(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Subtract(int64(1), int64(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Subtract(1, 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

-1

Code:

out, err := Subtract(time.Now(), time.Hour*2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Code:

out, err := Subtract(uint8(4), uint8(2))
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

2

func Sum Uses

func Sum(in interface{}) (interface{}, error)

Sum returns the total value from the array or slice of ints, floats, or durations.

Code:

out, err := Sum([]time.Duration{
    time.Hour * 2,
    time.Hour * 1,
    time.Hour * 3,
})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

6h0m0s

Code:

out, err := Sum([]float64{1.11, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

3.33

Code:

out, err := Sum([]interface{}{10, 2.22})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

12.22

Code:

out, err := Sum([]int{10, 20})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

30

Code:

out, err := Sum([]uint8{1, 2, 3})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

6

type ErrInvalidAddition Uses

type ErrInvalidAddition struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidAddition) Error Uses

func (e ErrInvalidAddition) Error() string

Error returns the error formatted as a string.

type ErrInvalidComparison Uses

type ErrInvalidComparison struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidComparison) Error Uses

func (e ErrInvalidComparison) Error() string

Error returns the error formatted as a string.

type ErrInvalidDivision Uses

type ErrInvalidDivision struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidDivision) Error Uses

func (e ErrInvalidDivision) Error() string

Error returns the error formatted as a string.

type ErrInvalidKind Uses

type ErrInvalidKind struct {
    Value    reflect.Type
    Expected []reflect.Kind
}

func (ErrInvalidKind) Error Uses

func (e ErrInvalidKind) Error() string

Error returns the error formatted as a string.

type ErrInvalidMultiplication Uses

type ErrInvalidMultiplication struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidMultiplication) Error Uses

func (e ErrInvalidMultiplication) Error() string

Error returns the error formatted as a string.

type ErrInvalidPower Uses

type ErrInvalidPower struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidPower) Error Uses

func (e ErrInvalidPower) Error() string

Error returns the error formatted as a string.

type ErrInvalidSubtraction Uses

type ErrInvalidSubtraction struct {
    A   interface{}
    B   interface{}
}

func (ErrInvalidSubtraction) Error Uses

func (e ErrInvalidSubtraction) Error() string

Error returns the error formatted as a string.

Package math imports 5 packages (graph) and is imported by 1 packages. Updated 2019-08-20. Refresh now. Tools for package owners.