mathutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: CC0-1.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const MaxInt = int64(int(^uint(0) >> 1))

MaxInt defines the maximum size of an int.

View Source
const MaxUint = uint64(^uint(0))

MaxUint defines the maximum size of a uint.

View Source
const MinInt = -MaxInt - 1

MinInt defines the minimum size of an int.

Variables

This section is empty.

Functions

func AddInt16

func AddInt16(a int16, b int16) (int16, bool)

AddInt16 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddInt32

func AddInt32(a int32, b int32) (int32, bool)

AddInt32 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddInt64

func AddInt64(a int64, b int64) (int64, bool)

AddInt64 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddInt8

func AddInt8(a int8, b int8) (int8, bool)

AddInt8 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddModInt64

func AddModInt64(a int64, b int64, m int64) int64

AddModInt64 returns a+b modulo m. The modulus m is required to be positive; this will panic otherwise.

func AddUint16

func AddUint16(a uint16, b uint16) (uint16, bool)

AddUint16 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddUint32

func AddUint32(a uint32, b uint32) (uint32, bool)

AddUint32 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddUint64

func AddUint64(a uint64, b uint64) (uint64, bool)

AddUint64 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func AddUint8

func AddUint8(a uint8, b uint8) (uint8, bool)

AddUint8 calculates a+b with accumulation at the maximum and minimum values for their type. That is, it returns a+b,true if the sum lies with the range for the type, and max/min of range,false otherwise.

func DivModInt64

func DivModInt64(a int64, b int64) (int64, int64)

DivModInt64 returns q, r where a = q b+r and 0 <= r < b. We require that b is positive, and panic if this is not the case

func DotProductInt16

func DotProductInt16(S1 []int16, S2 []int16, x *big.Int) (int16, bool, *big.Int)

DotProductInt16 attempts to return the dot-product of the slices S1 and S2. That is, it returns S1[0] * S2[0] + ... + S1[k] * S2[k], where k+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is 0. If the dot-product fits in an int16, then this will be returned followed by true, nil. If the dot-product does not fit in an int16, then the min/max will be returned followed by false, x. The argument x is an optional *big.Int which will be set to the value of the dot-product and returned as the third return value only if the argument x is non-nil and the result does not fit in an int16.

func DotProductInt32

func DotProductInt32(S1 []int32, S2 []int32, x *big.Int) (int32, bool, *big.Int)

DotProductInt32 attempts to return the dot-product of the slices S1 and S2. That is, it returns S1[0] * S2[0] + ... + S1[k] * S2[k], where k+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is 0. If the dot-product fits in an int32, then this will be returned followed by true, nil. If the dot-product does not fit in an int32, then the min/max will be returned followed by false, x. The argument x is an optional *big.Int which will be set to the value of the dot-product and returned as the third return value only if the argument x is non-nil and the result does not fit in an int32.

func DotProductInt64

func DotProductInt64(S1 []int64, S2 []int64, x *big.Int) (int64, bool, *big.Int)

DotProductInt64 attempts to return the dot-product of the slices S1 and S2. That is, it returns S1[0] * S2[0] + ... + S1[k] * S2[k], where k+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is 0. If the dot-product fits in an int64, then this will be returned followed by true, nil. If the dot-product does not fit in an int64, then the min/max will be returned followed by false, x. The argument x is an optional *big.Int which will be set to the value of the dot-product and returned as the third return value only if the argument x is non-nil and the result does not fit in an int64.

func DotProductInt64ModInt64

func DotProductInt64ModInt64(S1 []int64, S2 []int64, m int64) int64

DotProductInt64ModInt64 returns the dot-product of the slices S1 and S2, computed modulo m. That is, it returns S1[0] * S2[0] + ... + S1[n] * S2[n] mod m, where n+1 is the minimum of len(S1) and len(S2). The dot-product of two empty slices is the zero element. DotProductInt64ModInt64 will panic if m is not positive.

func ExpModInt64

func ExpModInt64(a int64, k int64, m int64) int64

ExpModInt64 returns a^k mod m. m is required to be positive; this will panic otherwise. k is required to be non-negative; this will panic otherwise. The return value is always in the range [0,m-1].

func MulModInt64

func MulModInt64(a int64, z int64, m int64) int64

MulModInt64 returns a*z mod m. m is required to be positive; this will panic otherwise. The return value is always in the range [0,m-1].

func MulModInt64Trial

func MulModInt64Trial(a int64, b int64, m int64) int64

MulModInt64Trial returns a*b mod m. m is required to be positive; this will panic otherwise. The return value is always in the range [0,m-1].

func MulThenAddModInt64

func MulThenAddModInt64(a int64, b int64, c int64, m int64) int64

MulThenAddModInt64 returns a*b+c modulo m. The modulus m is required to be positive; this will panic otherwise.

func MultiplyInt16

func MultiplyInt16(a int16, b int16) (int16, bool)

MultiplyInt16 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyInt32

func MultiplyInt32(a int32, b int32) (int32, bool)

MultiplyInt32 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyInt64

func MultiplyInt64(a int64, b int64) (int64, bool)

MultiplyInt64 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyInt8

func MultiplyInt8(a int8, b int8) (int8, bool)

MultiplyInt8 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyUint16

func MultiplyUint16(a uint16, b uint16) (uint16, bool)

MultiplyUint16 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyUint32

func MultiplyUint32(a uint32, b uint32) (uint32, bool)

MultiplyUint32 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyUint64

func MultiplyUint64(a uint64, b uint64) (uint64, bool)

MultiplyUint64 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func MultiplyUint8

func MultiplyUint8(a uint8, b uint8) (uint8, bool)

MultiplyUint8 calculates a*b with accumulation at the maximum and minimum values for their type. That is, it returns a*b,true if the product lies with the range for the type, and max/min of range,false otherwise.

func PowerInt64

func PowerInt64(a int64, b int64) (int64, bool)

PowerInt64 calculates a^b. It returns a^b,true if the power could be computed, and 0,false otherwise. This will panic if the exponent b is < 0 and a is not a unit. Important: This function guarantees correctness of the result, however a return value of 0,false does NOT necessarily mean that the result doesn't fit in an int64.

func ProductInt64ModInt64

func ProductInt64ModInt64(S []int64, m int64) int64

ProductInt64ModInt64 returns the product of S computed modulo m. The product of the empty slice is 1.

func QuotientAndRemainderInt64

func QuotientAndRemainderInt64(a int64, b int64) (q int64, r int64)

QuotientAndRemainderInt64 returns the quotient q and remainder r such that a = q b+r, where 0 <= r < b if b > 0, and b < r <= 0 if b < 0. This will panic if b is zero.

func SubtractInt16

func SubtractInt16(a int16, b int16) (int16, bool)

SubtractInt16 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractInt32

func SubtractInt32(a int32, b int32) (int32, bool)

SubtractInt32 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractInt64

func SubtractInt64(a int64, b int64) (int64, bool)

SubtractInt64 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractInt8

func SubtractInt8(a int8, b int8) (int8, bool)

SubtractInt8 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractUint16

func SubtractUint16(a uint16, b uint16) (uint16, bool)

SubtractUint16 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractUint32

func SubtractUint32(a uint32, b uint32) (uint32, bool)

SubtractUint32 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractUint64

func SubtractUint64(a uint64, b uint64) (uint64, bool)

SubtractUint64 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SubtractUint8

func SubtractUint8(a uint8, b uint8) (uint8, bool)

SubtractUint8 calculates a-b with accumulation at the maximum and minimum values for their type. That is, it returns a-b,true if the difference lies with the range for the type, and max/min of range,false otherwise.

func SumInt64ModInt64

func SumInt64ModInt64(S []int64, m int64) int64

SumInt64ModInt64 returns the sum of S computed modulo m. The sum of the empty slice is zero.

func XGCDOfPairInt64

func XGCDOfPairInt64(a int64, b int64) (int64, int64, int64)

XGCDOfPairInt64 returns the greatest common divisor of a and b, along with integers u and v such that u a + v b = gcd. The gcd is always non-negative. The gcd of 0 and n is defined to be |n|, so in particular, the gcd of 0 and 0 is 0. We require that a and b are both greater than math.MinInt64 (=-2^63) as otherwise the calculation could overflow an int64; it panics in this case.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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