math

package
v0.0.0-...-b3872f7 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

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

Constants

This section is empty.

Variables

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

Functions

func Add

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.

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

3h0m0s
Example (Floats)
out, err := Add(1.11, 2.22)
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:

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

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

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

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

3

func Compare

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.

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

-1
Example (Floats)
out, err := Compare(1.11, 2.22)
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:

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

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

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

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

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

-1

func Divide

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.

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

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

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

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

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

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

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

2

func Max

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

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

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

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

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

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

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

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

3

func Mean

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.

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

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

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

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

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

2

func Min

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

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

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

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

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

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

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

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

1

func Multiply

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.

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

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

2h0m0s
Example (Floats)
out, err := Multiply(0.5, 3.0)
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:

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

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

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

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

2

func Pow

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.

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

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

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

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

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

16

func Product

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

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

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

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

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

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

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

6

func Subtract

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.

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

1h0m0s
Example (Floats)
out, err := Subtract(1.11, 2.22)
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:

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

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

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

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

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

2

func Sum

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

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

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

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

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

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

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

6

Types

type ErrInvalidAddition

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

func (ErrInvalidAddition) Error

func (e ErrInvalidAddition) Error() string

Error returns the error formatted as a string.

type ErrInvalidComparison

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

func (ErrInvalidComparison) Error

func (e ErrInvalidComparison) Error() string

Error returns the error formatted as a string.

type ErrInvalidDivision

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

func (ErrInvalidDivision) Error

func (e ErrInvalidDivision) Error() string

Error returns the error formatted as a string.

type ErrInvalidKind

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

func (ErrInvalidKind) Error

func (e ErrInvalidKind) Error() string

Error returns the error formatted as a string.

type ErrInvalidMultiplication

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

func (ErrInvalidMultiplication) Error

func (e ErrInvalidMultiplication) Error() string

Error returns the error formatted as a string.

type ErrInvalidPower

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

func (ErrInvalidPower) Error

func (e ErrInvalidPower) Error() string

Error returns the error formatted as a string.

type ErrInvalidSubtraction

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

func (ErrInvalidSubtraction) Error

func (e ErrInvalidSubtraction) Error() string

Error returns the error formatted as a string.

Jump to

Keyboard shortcuts

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