Documentation ¶
Overview ¶
Package arithmetic provides arithmetic operations for any type.
The four elemental operations (addition, subtraction, multiplication and division) are defined as functions with their short names (Add, Sub, Mul and Div). Other helper functions are provided too. Since there are some data types without an arithmetical representation, some rules are applied during the value extraction (see GetVal).
Index ¶
- func Add(operanders ...interface{}) float64
- func Div(operanders ...interface{}) float64
- func Eq(operanders ...interface{}) bool
- func GetVal(operander interface{}) float64
- func Mul(operanders ...interface{}) float64
- func Ne(operanders ...interface{}) bool
- func Sub(operanders ...interface{}) float64
- type Operander
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add(operanders ...interface{}) float64
Add gets any number of elements and returns their addition.
func Div ¶
func Div(operanders ...interface{}) float64
Div gets any number of elements and returns their division.
func Eq ¶
func Eq(operanders ...interface{}) bool
Eq gets any number of elements and checks if they are equals.
func GetVal ¶
func GetVal(operander interface{}) float64
GetVal extracts the arithmetic representation from any type. It is ruled by the value extraction rules.
Value extraction rules ¶
1. Any element that satisfies the Operander interface will obtain its value from the Val method.
2. Any element with a named type that doesn't satisfies the Operander interface will obtain its value from its underlying type.
3. Boolean elements with a true value will be represented as 1, for false values they will be 0.
4. Numeric elements (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, byte, rune) will be converted to float64, but complex numbers will be represented by their real part in float64 form.
5. Composed (arrays, maps, slices, strings, structs) and channel elements will be represented by their length (or their number of fields for structs).
6. Any other element will be 0.
func Mul ¶
func Mul(operanders ...interface{}) float64
Mul gets any number of elements and returns their multiplication.
Types ¶
type Operander ¶
type Operander interface {
Val() float64
}
Operander is the interface that wraps the arithmetic representation method. It is useful for adding custom behavior to named types when GetVal processes it, other wise, the underlying type is obtained and follows the extraction rules.
Val returns the variable's arithmetic representation (float64).
Example ¶
package main import ( "fmt" "nt.web.ve/go/ntgo/reflect/arithmetic" ) // BytesSum is a simple string implementation of arithmetic.Operander. Its // arithmetic representation is the sum of all its bytes values. type BytesSum string func (o BytesSum) Val() (r float64) { for _, v := range o { r += float64(v) } return r } func main() { x := BytesSum("hello") fmt.Println(x.Val()) fmt.Println(arithmetic.Add(x, BytesSum("world"))) fmt.Println(arithmetic.Sub(x, 32)) fmt.Println(arithmetic.Mul(x, "world")) fmt.Println(arithmetic.Div(x, []byte{'M', 'A'})) fmt.Println(arithmetic.Add(x)) fmt.Println(arithmetic.Sub(x)) fmt.Println(arithmetic.Mul(x)) fmt.Println(arithmetic.Div(x)) }
Output: 532 1084 500 2660 266 532 532 532 532