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.

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.

Example
package main

import (
	"fmt"

	"go.ntrrg.dev/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

Index

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 Mul

func Mul(operanders ...interface{}) float64

Mul gets any number of elements and returns their multiplication.

func Ne

func Ne(operanders ...interface{}) bool

Ne gets any number of elements and checks if they are differents.

func Sub

func Sub(operanders ...interface{}) float64

Sub gets any number of elements and returns their subtraction.

func Val

func Val(operander interface{}) float64

Val extracts the arithmetic representation from any type. It is ruled by the value extraction rules.

Types

type Operander

type Operander interface {
	Val() float64
}

An Operander is a value that can be represented as an arithmetic operand.