go-bigger

command module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 2 Imported by: 0

README

go-biggier

a go lang big integer lib

refer to java BigInteger

In the go language, we know that there are only byte, int32, and int64 for integers, float have only float32 and float64. Sometimes we need to use large number operations according to your needs. The big package in go with NewInt and NewFloat, but there are fewer APIs, and only basic types can be used for initialization. You cannot use a string for initialization. So I developed go-bigger that reference to Java's large number classes and provided a rich API calls.

1. Import module

go get github.com/sineycoder/go-bigger

2. BigInteger

In BigInteger, we cached |x| < 16 BigInteger

you can use big_integer.NewBigIntegerInt(1231221) or big_integer.ValueOf(6782613786431) to initialize a BigInteger. If use ValueOf and within 16, it returns a cache BigInteger.

2.1 Add
func main() {
	a := bigger.BigIntegerValueOf(6782613786431)
	b := bigger.BigIntegerValueOf(-678261378231)
	res := a.Add(b)
	fmt.Println(res.String())
}

// result:6104352408200
2.2 Subtract
func main() {
	a := bigger.BigIntegerValueOf(6782613786431)
	b := bigger.BigIntegerValueOf(-678261378231)
	res := a.Subtract(b)
	fmt.Println(res.String())
}

// result:7460875164662
2.3 Divide
func main() {
	a := bigger.BigIntegerValueOf(6782613786431)
	b := bigger.BigIntegerValueOf(-678261378231)
	res := a.Divide(b)
	fmt.Println(res.String())
}

// result: -10
2.4 Multiply
func main() {
	a := bigger.BigIntegerValueOf(6782613786431)
	b := bigger.BigIntegerValueOf(-678261378231)
	res := a.Multiply(b)
	fmt.Println(res.String())
}

// result:-4600384974793271546583561

3.BigDecimal

you can use bigger.NewBigDecimalString("123123.111") or bigger.BigDecimalValueOf(6782613786431.111) to initialize a BigInteger. If use BigDecimalValueOf and whithin 10, it returns a chache BigDecimal.

3.1 Add
func main() {
	a := bigger.BigDecimalValueOf(6782613786431)
	b := bigger.BigDecimalValueOf(-678261378231)
	res := a.Add(b)
	fmt.Println(res.String())
}

// result:6104352408200
3.2 Subtract
func main() {
	a := bigger.BigDecimalValueOf(6782613786431)
	b := bigger.BigDecimalValueOf(-678261378231)
	res := a.Subtract(b)
	fmt.Println(res.String())
}

// result:7460875164662
3.3 Divide
func main() {
	a := bigger.BigDecimalValueOf(6782613786431)
	b := bigger.BigDecimalValueOf(-678261378231)
	res := a.Divide(b, 12, bigger.ROUND_HALF_UP)
	fmt.Println(res.String())
}

// result: -10.000000006076 
3.4 Multiply
func main() {
	a := bigger.BigDecimalValueOf(6782613786431)
	b := bigger.BigDecimalValueOf(-678261378231)
	res := a.Multiply(b)
	fmt.Println(res.String())
}

// result:-4600384974793271546583561

if you want to set a precision, you can use setScale(). Remenber: The return value must be assigned. (e.g. res = res.setScale(12, bigger.ROUND_HALF_UP))

The details please visit this WebSite:https://blog.csdn.net/a568283992/article/details/119698329

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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