mathkit

package
v0.299.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const ErrParseBigInt errorkit.Error = "ErrParseBigInt"

Variables

This section is empty.

Functions

func CanIntMulOverflow

func CanIntMulOverflow[INT Int](x, y INT) bool

func CanIntSumOverflow

func CanIntSumOverflow[INT Int](a, b INT) bool

func MaxInt

func MaxInt[T Int]() T
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	_ = mathkit.MaxInt[int8]()  // 127
	_ = mathkit.MaxInt[int16]() // 32767
	_ = mathkit.MaxInt[int32]() // 2147483647
	_ = mathkit.MaxInt[int64]() // 9223372036854775807

	_ = mathkit.MaxInt[time.Duration]() // time.Duration(9223372036854775807)
}

func MinInt

func MinInt[T Int]() T
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	_ = mathkit.MinInt[int8]()  // -128
	_ = mathkit.MinInt[int16]() // -32768
	_ = mathkit.MinInt[int32]() // -2147483648
	_ = mathkit.MinInt[int64]() // -9223372036854775808

	_ = mathkit.MinInt[time.Duration]() // time.Duration(-9223372036854775808)
}

func SumInt

func SumInt[INT Int](a, b INT) (INT, bool)

Types

type AInt

type AInt = uint64

func AbsInt

func AbsInt[N Int](n N) AInt
Example
package main

import (
	"math"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	_ = mathkit.AbsInt(math.MinInt32)
	// MinInt32      == -2147483648
	// Abs(MinInt32) == 2147483648
	_ = mathkit.AbsInt(math.MaxInt32)
	// MaxInt32      == 2147483647
	// Abs(MaxInt32) == 2147483647
}

type BigInt

type BigInt[INT Int] struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(24 * time.Hour))
	v = v.Mul(v.Of(365))
	v = v.Mul(v.Of(1024))
	_ = v
}

func (BigInt[INT]) Abs

func (i BigInt[INT]) Abs() BigInt[INT]

func (BigInt[INT]) Add

func (i BigInt[INT]) Add(n BigInt[INT]) BigInt[INT]
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(24 * time.Hour))
}

func (BigInt[INT]) Compare

func (i BigInt[INT]) Compare(o BigInt[INT]) int

func (BigInt[INT]) Div

func (i BigInt[INT]) Div(n BigInt[INT]) BigInt[INT]
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(24 * time.Hour))
	v = v.Div(v.Of(2))
}

func (BigInt[INT]) FromBigInt

func (i BigInt[INT]) FromBigInt(n *big.Int) BigInt[INT]

func (BigInt[INT]) IsZero

func (i BigInt[INT]) IsZero() bool

func (BigInt[INT]) Iter

func (i BigInt[INT]) Iter() iter.Seq[INT]

Iter produces non-zero integer values ranging from 1 to the maximum or minimum possible integer value. When you add up all these yielded values, they equal the current big integer being iterated over.

Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(mathkit.MaxInt[time.Duration]()))
	v = v.Add(v.Of(mathkit.MaxInt[time.Duration]()))
	v = v.Add(v.Of(mathkit.MaxInt[time.Duration]()))

	for n := range v.Iter() {
		// n will contain a non-zero Int<time.Duration> value,
		// and the total sum of the iterated n values will be equal to the value of v.
		_ = n //
	}
}

func (BigInt[INT]) Mul

func (i BigInt[INT]) Mul(n BigInt[INT]) BigInt[INT]
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(24 * time.Hour))
	v = v.Mul(v.Of(365))
}

func (BigInt[INT]) Of

func (i BigInt[INT]) Of(n INT) BigInt[INT]

func (BigInt[INT]) Parse

func (BigInt[INT]) Parse(raw string) (BigInt[INT], error)

func (BigInt[INT]) String

func (i BigInt[INT]) String() string
Example
package main

import (
	"time"

	"go.llib.dev/frameless/pkg/mathkit"
)

func main() {
	var v mathkit.BigInt[time.Duration]
	v = v.Add(v.Of(24 * time.Hour))
	_ = v.String()
}

func (BigInt[INT]) Sub

func (i BigInt[INT]) Sub(n BigInt[INT]) BigInt[INT]

func (BigInt[INT]) ToBigInt

func (i BigInt[INT]) ToBigInt() *big.Int

func (BigInt[INT]) ToInt

func (i BigInt[INT]) ToInt() (INT, bool)

type Float

type Float constraints.Float

type Int

type Int constraints.Int

type Number

type Number constraints.Number

type UInt

type UInt constraints.UInt

Jump to

Keyboard shortcuts

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