math

package
v0.0.0-...-02bf512 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package math provides useful math functions

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var (
	OverflowErr  = fmt.Errorf("Overflow error")
	UnderflowErr = fmt.Errorf("Underflow error")
	DivByZeroErr = fmt.Errorf("Division by zero error")
)

Constants

Functions

func Abs

func Abs[T constraint.Numeric](val *T) error

Abs calculates the absolute value of any numeric type. Integer types have a range of -N ... +(N-1), which means that if you try to calculate abs(-N), the result is -N. The reason for this is that there is no corresponding +N, that would require more bits. In this special case, an error is returned, otherwise nil is returned. Note that while the constraint allows unsigned ints for completeness, no operation is performed.

func AddInt

func AddInt[T constraint.SignedInteger](ae1 T, ae2 *T) error

AddInt adds two signed integers overwriting the second value, and returns an error if over/underflow occurs. Over/underflow occurrs if two numbers of the same sign are added, and the sign of result has changed. EG, two positive ints are added to create a result too large to be represented in the same number of bits,

or two negative ints are added to create a result too small to be represented in the same number of bits.

func AddUint

func AddUint[T constraint.UnsignedInteger](ae1 T, ae2 *T) error

AddUint adds two unsigned integers overwriting the second value, and returns an error if overflow occurs. Overflow occurs if two numbers are added, and the magnitude of result is smaller.

func AdjustDecimalFormat

func AdjustDecimalFormat(d1, d2 Decimal) (string, string)

AdjustDecimalFormat adjusts the two decimals strings to have the same number of digits before and the decimal, and the same number of digits after the decimal. Leading and trailing zeros are added as needed. Since minus has a higher ASCII value than plus, a positive number has a leading slash.

The strings returned are almost comparable: "-2" > "-1".

Examples: 30, 5 -> " 30", " 05" 1.23, -78.295 -> " 01.230", "-78.295"

func AdjustDecimalScale

func AdjustDecimalScale(d1, d2 *Decimal) error

AdjustDecimalScale adjusts the scale of d1 and d2:

  • If both numbers have the same scale, no adjustment is made
  • Otherwise, the number with the smaller scale is usually adjusted to the same scale as the other number
  • Increasing the scale can cause some most significant digits to be lost, in which case the other number is rounded down to match the scale

Examples:

1.5 and 1.25 -> 1.50 and 1.25 1.5 and 18 digits with no decimals -> 18 digits cannot increase scale, so round 1.5 to 2 99_999_999_999_999_999.5 and 1 -> the 18 digits round to a 19 digit value, an error occurs

func CmpComplex

func CmpComplex[T constraint.Complex](val1, val2 T) (res int)

CmpComplex compares two complex types and returns -1, 0, 1, depending on whether val1 is <, =, or > val2.

func CmpOrdered

func CmpOrdered[T constraint.Ordered](val1, val2 T) (res int)

CmpOrdered compares two ordered types and returns -1, 0, 1, depending on whether val1 is <, =, or > val2.

func Div

func Div[T constraint.Numeric](dividend T, divisor T, quotient *T) error

Div calculates the quotient of a division operation of a pair of integers or a pair of floating point types. In the case of integers, the remainder is handled as follows: - If the divisor is even, and abs(remainder) >= abs(divisor) / 2, then increase magnitude of quotient by one. - If the divisor is odd, and abs(remainder) > abs(divisor) / 2, then increase magnitude of quotient by one.

Increasing the magnitude by one means add one of the quotient is positive, subtract one if negative. The purpose of adjusting the magnitude is to get the same result as rounding the floating point calculation. Floats are not used since 32 and 64 bit integer values can have a magnitude too large to be expressed accurately in a float.

Integer Examples: 18 / 4 = 4 remainder 2. Since divisor 4 is even and remainder 2 is >= (4 / 2 = 2), increment quotient to 5 (round 4.5 up). 17 / 5 = 3 remainder 2. Since divisor 5 is odd and remainder 2 is not > (5 / 2 = 2), leave quotient as is (round 3.4 down).

func DivBigOps

func DivBigOps[T constraint.BigOps[T]](dividend T, divisor T, quotient *T) error

DivBigOps is the BigOps version of Div

func MaxCmp

func MaxCmp[T constraint.Cmp[T]](val1, val2 T) T

MaxCmp returns the maximum value of two comparable types

func MaxComplex

func MaxComplex[T constraint.Complex](val1, val2 T) T

MaxComplex returns the maximum value of two complex types

func MaxOrdered

func MaxOrdered[T constraint.Ordered](val1, val2 T) T

MaxOrdered returns the maximum value of two ordered types

func MinCmp

func MinCmp[T constraint.Cmp[T]](val1, val2 T) T

MinCmp returns the minimum value of two comparable types

func MinComplex

func MinComplex[T constraint.Complex](val1, val2 T) T

MinComplex returns the minimum value of two complex types

func MinOrdered

func MinOrdered[T constraint.Ordered](val1, val2 T) T

MinOrdered returns the minimum value of two ordered types

func Mul

func Mul[T constraint.Integer](mp T, ma *T) error

Mul multiples two integers overwriting the second value, and returns an error if over/underflow occurs. Over/underflow occurs if the magnitude of the result requires more bits than the type provides. Unsigned types can only overflow.

func SubInt

func SubInt[T constraint.SignedInteger](me T, sed *T) error

SubInt subtracts two integers as sed = me - sed, overwriting the second value, and returns an error if over/underflow occurs. See AddInt.

func SubUint

func SubUint[T constraint.UnsignedInteger](me T, sed *T) error

SubUint subtracts two integers as sed = me - sed, overwriting the second value, and returns an error if underflow occurs. Underflow occurs if sed > me before the subtraction is performed.

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal is like SQL Decimal(precision, scale): - precision is always 18, the maximum number of decimal digits a signed 64 bit value can store - scale is number of digits after decimal place, must be <= 18 (default 2 as most popular use is money)

The zero value is ready to use

func MustDecimal

func MustDecimal(value int64, scale ...uint) Decimal

MustDecimal is a must version of OfDecimal

func OfDecimal

func OfDecimal(value int64, scale ...uint) (d Decimal, err error)

OfDecimal creates a Decimal with the given sign, digits, and optional scale (default 0)

func StringToDecimal

func StringToDecimal(value string) (d Decimal, err error)

StringToDecimal creates a Decimal from the given string The string must contain no more than 18 significant digits (leading zeros are not allowed), and satisfy the following regex: -?[1-9][0-9]*(.[0-9]+)?

func (Decimal) Add

func (d Decimal) Add(o Decimal) (Decimal, error)

Add adds two decimals together by first adjusting them to the same scale, then adding their values Returns an error if: - Adjusting the scale produces an error - Addition overflows or underflows

func (Decimal) Cmp

func (d Decimal) Cmp(o Decimal) int

Cmp compares d against o, and returns -1, 0, or 1 depending on whether d < o, d = o, or d > o, respectively.

func (Decimal) DivIntAdd

func (d Decimal) DivIntAdd(o uint) ([]Decimal, error)

DintIntAdd is like DivIntQuoRem, except that it returns a slice of values that add up to d. EG, 100.00 / 3 = [33.34, 33.33, 33.33]. This method just calls DivIntQuoRem and spreads the remainder across the first remainder values returned.

func (Decimal) DivIntQuoRem

func (d Decimal) DivIntQuoRem(o uint) (Decimal, Decimal, error)

DivIntQuoRem divides d by unsigned integer o, and returns (quotient, remainder, error). The scale of the quotient and remainder are the same as that of d. EG, 100.00 / 3 = 33.33 remainder 0.01.

The divisor o cannot be larger than the dividend d.

Returns a division by zero error if o is zero. Returns a divisor too large error if the o > d.value.

func (Decimal) Mul

func (d Decimal) Mul(o Decimal) (Decimal, error)

Mul multiplies d by o, then sets the result scale to (d scale) + (o scale) Returns an overflow error if the result > 18 9 digits.

func (Decimal) MustAdd

func (d Decimal) MustAdd(o Decimal) Decimal

MustAdd is amjust version of Add

func (Decimal) MustDivIntAdd

func (d Decimal) MustDivIntAdd(o uint) []Decimal

MustDivIntAdd is a must version of DivIntAdd

func (Decimal) MustDivIntQuoRem

func (d Decimal) MustDivIntQuoRem(o uint) (Decimal, Decimal)

MustDivIntQuoRem is a must version of DivIntQuoRem

func (Decimal) MustMul

func (d Decimal) MustMul(o Decimal) Decimal

MustMul is a must version of Mul

func (Decimal) MustSub

func (d Decimal) MustSub(o Decimal) Decimal

MustSub is amjust version of Sub

func (Decimal) Negate

func (d Decimal) Negate() Decimal

Negate returns the negation of d. If 0 is passed, the result is 0.

func (Decimal) Precision

func (d Decimal) Precision() int

Precison returns the total number of digits of a decimal, including trailing zeros. Effectively, the length of the decimal as a string, without a minus sign or decimal point.

func (Decimal) Scale

func (d Decimal) Scale() uint

Scale returns the number of digits after the decimal, if any.

func (Decimal) Sign

func (d Decimal) Sign() (sgn int)

Sign returns the sign of the number: -1 if value < 0

0 if value = 0

+1 if value > 0

func (Decimal) String

func (d Decimal) String() (str string)

String is the Stringer interface

func (Decimal) Sub

func (d Decimal) Sub(o Decimal) (Decimal, error)

Sub subtracts o from d by first adjusting them to the same scale, then subtracting their values Returns an error if: - Adjusting the scale produces an error - Subtraction overflows or underflows

type Range

type Range[T constraint.IntegerAndFloat] struct {
	// contains filtered or unexported fields
}

Range represents a range of values. The range may be open, half open, or closed.

func OfRange

func OfRange[T constraint.IntegerAndFloat](
	min T,
	minMode RangeMode,
	max T,
	maxMode RangeMode,
	initial T,
) Range[T]

OfRange constructs a range from minimum value and mode, maximum value and mode, and initial value. An initial value is required since min and max could both be open and the type could a float, so there is no sensible default initial value.

Panics if the initial value is not in the specified range.

func (Range[T]) GetMax

func (r Range[T]) GetMax() (T, RangeMode)

GetMax returns the maximum value and mode

func (Range[T]) GetMin

func (r Range[T]) GetMin() (T, RangeMode)

GetMin returns the minimum value and mode

func (Range[T]) GetValue

func (r Range[T]) GetValue() T

GetValue returns the current value

func (*Range[T]) SetValue

func (r *Range[T]) SetValue(val T) error

SetValue sets the value.

Returns an error if the value cannot be set because it is outside the defined bounds, otherwise returns nil

type RangeMode

type RangeMode bool

RangeMode indicates if a minimum or maximum value in a range is open or closed

const (
	Open   RangeMode = false
	Closed RangeMode = true
)

Jump to

Keyboard shortcuts

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