goarith

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: MIT Imports: 6 Imported by: 3

README

A package for general numeric arithmetic in Go

This package, goarith, implements mixed mode arithmetic of int32, int64, float64 and *big.Int.

The package defines four concrete types:

type Int32 int32
type Int64 int64
type Float64 float64
type BigInt big.Int

Int32, Int64, Float64 and *BigInt implement Number:

// Number is a general numeric type.
type Number interface {
	// String returns a string representation of the number.
	String() string

	// Int returns the int value for this and a bool indicating whether
	// the int value represents this exactly.
	Int() (i int, exact bool)

	// Add adds this and b (i.e. it return this + b).
	Add(b Number) Number

	// Sub subtracts b from this (i.e. it returns this - b).
	Sub(b Number) Number

	// Cmp compares this and b and returns:
	//
	// -1 if this <  b
	//  0 if this == b
	//  1 if this >  b
	//
	Cmp(b Number) int

	// Mul multiplies this by b (i.e. it returns this * b).
	Mul(b Number) Number

	// RQuo returns the rounded quotient of this and b.
	RQuo(b Number) Float64

	// QuoRem returns the quotient and the remainder of this and b.
	// The quotient will be an Int32, Int64 or BigInt.
	QuoRem(b Number) (quotient Number, remainder Number)
}

Example

The following example computes factorial numbers.

package main

import (
	"fmt"
	"github.com/nukata/goarith"
)

func main() {
	var a goarith.Number = goarith.Int32(1)
	for i := goarith.Int32(2); i <= 30; i++ {
		a = a.Mul(i)
		if i%10 == 0 {
			fmt.Printf("%3d %T\t%s\n", i, a, a.String())
		}
	}
}

It will print the output as follows.

 10 goarith.Int32	3628800
 20 goarith.Int64	2432902008176640000
 30 *goarith.BigInt	265252859812191058636308480000000

Documentation

Overview

Package goarith implements general numeric arithmetic.

Index

Examples

Constants

View Source
const (
	MaxInt = (1<<bits.UintSize)/2 - 1
	MinInt = (1 << bits.UintSize) / -2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BigInt

type BigInt big.Int

*BigInt implements Number.

Example (Factorial)
a := AsNumber(1)
for i := Int64(2); i <= 40; i++ {
	a = a.Mul(i)
	if i%10 == 0 {
		fmt.Printf("%3d %T \t%s\n", i, a, a.String())
	}
}
Output:

 10 goarith.Int32 	3628800
 20 goarith.Int64 	2432902008176640000
 30 *goarith.BigInt 	265252859812191058636308480000000
 40 *goarith.BigInt 	815915283247897734345611269596115894272000000000
Example (Fibonacci)
var a Number = Int32(0)
var b Number = Int32(1)
for i := 0; i <= 100; i++ {
	if i%10 == 0 {
		fmt.Printf("%3d %T \t%s\n", i, a, a.String())
	}
	a = a.Add(b)
	a, b = b, a
}
Output:

  0 goarith.Int32 	0
 10 goarith.Int32 	55
 20 goarith.Int32 	6765
 30 goarith.Int32 	832040
 40 goarith.Int32 	102334155
 50 goarith.Int64 	12586269025
 60 goarith.Int64 	1548008755920
 70 goarith.Int64 	190392490709135
 80 goarith.Int64 	23416728348467685
 90 goarith.Int64 	2880067194370816120
100 *goarith.BigInt 	354224848179261915075

func (*BigInt) Add

func (a *BigInt) Add(b Number) Number

func (*BigInt) Cmp

func (a *BigInt) Cmp(b Number) int

func (*BigInt) Int added in v0.2.0

func (a *BigInt) Int() (int, bool)
Example
x := big.NewInt(100)
a := (*BigInt)(x)
i, b := a.Int()
fmt.Printf("%d %t\n", i, b)
x.SetString("123456789012345678901234567890", 0)
i, b = a.Int()
fmt.Printf("%t %t\n", i == MaxInt, b)
Output:

100 true
true false

func (*BigInt) Mul

func (a *BigInt) Mul(b Number) Number

func (*BigInt) QuoRem

func (a *BigInt) QuoRem(b Number) (Number, Number)
Example
q, r := (*BigInt)(big.NewInt(13)).QuoRem(Int64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = (*BigInt)(big.NewInt(-13)).QuoRem(Int64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = (*BigInt)(big.NewInt(13)).QuoRem(Int64(-4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output:

goarith.Int32 3, goarith.Int32 1
goarith.Int32 -3, goarith.Int32 -1
goarith.Int32 -3, goarith.Int32 1

func (*BigInt) RQuo

func (a *BigInt) RQuo(b Number) Float64

func (*BigInt) String

func (a *BigInt) String() string

func (*BigInt) Sub

func (a *BigInt) Sub(b Number) Number

type Float64

type Float64 float64

Float64 implements Number.

func (Float64) Add

func (a Float64) Add(b Number) Number

func (Float64) Cmp

func (a Float64) Cmp(b Number) int

func (Float64) Int added in v0.2.0

func (a Float64) Int() (int, bool)
Example
a := Float64(1.234)
i, b := a.Int()
fmt.Printf("%d %t", i, b)
Output:

1 false

func (Float64) Mul

func (a Float64) Mul(b Number) Number

func (Float64) QuoRem

func (a Float64) QuoRem(b Number) (Number, Number)
Example
q, r := Float64(13).QuoRem(Float64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Float64(-13).QuoRem(Float64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Float64(13).QuoRem(Float64(-4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Float64(13.4).QuoRem(Float64(1))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Float64(-13.4).QuoRem(Float64(1))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output:

goarith.Int32 3, goarith.Float64 1.0
goarith.Int32 -3, goarith.Float64 -1.0
goarith.Int32 -3, goarith.Float64 1.0
goarith.Int32 13, goarith.Float64 0.40000000000000036
goarith.Int32 -13, goarith.Float64 -0.40000000000000036

func (Float64) RQuo

func (a Float64) RQuo(b Number) Float64

func (Float64) String

func (a Float64) String() string
Example
var a Float64 = 1.234
fmt.Println(a.String())
fmt.Println(Float64(5.000).String())
Output:

1.234
5.0

func (Float64) Sub

func (a Float64) Sub(b Number) Number

type Int32

type Int32 int32

Int32 implements Number.

func (Int32) Add

func (a Int32) Add(b Number) Number

func (Int32) Cmp

func (a Int32) Cmp(b Number) int

func (Int32) Int added in v0.2.0

func (a Int32) Int() (int, bool)

func (Int32) Mul

func (a Int32) Mul(b Number) Number

func (Int32) QuoRem

func (a Int32) QuoRem(b Number) (Number, Number)

func (Int32) RQuo

func (a Int32) RQuo(b Number) Float64

func (Int32) String

func (a Int32) String() string

func (Int32) Sub

func (a Int32) Sub(b Number) Number

type Int64

type Int64 int64

Int64 implements Number.

func (Int64) Add

func (a Int64) Add(b Number) Number

func (Int64) Cmp

func (a Int64) Cmp(b Number) int

func (Int64) Int added in v0.2.0

func (a Int64) Int() (int, bool)

func (Int64) Mul

func (a Int64) Mul(b Number) Number

func (Int64) QuoRem

func (a Int64) QuoRem(b Number) (Number, Number)
Example
q, r := Int64(13).QuoRem(Int64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Int64(-13).QuoRem(Int64(4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
q, r = Int64(13).QuoRem(Int64(-4))
fmt.Printf("%T %s, %T %s\n", q, q.String(), r, r.String())
Output:

goarith.Int32 3, goarith.Int32 1
goarith.Int32 -3, goarith.Int32 -1
goarith.Int32 -3, goarith.Int32 1

func (Int64) RQuo

func (a Int64) RQuo(b Number) Float64

func (Int64) String

func (a Int64) String() string

func (Int64) Sub

func (a Int64) Sub(b Number) Number

type Number

type Number interface {
	// String returns a string representation of the number.
	String() string

	// Int returns the int value for this and a bool indicating whether
	// the int value represents this exactly.
	Int() (i int, exact bool)

	// Add adds this and b (i.e. it return this + b).
	Add(b Number) Number

	// Sub subtracts b from this (i.e. it returns this - b).
	Sub(b Number) Number

	// Cmp compares this and b and returns:
	//
	// -1 if this <  b
	//  0 if this == b
	//  1 if this >  b
	//
	Cmp(b Number) int

	// Mul multiplies this by b (i.e. it returns this * b).
	Mul(b Number) Number

	// RQuo returns the rounded quotient of this and b.
	RQuo(b Number) Float64

	// QuoRem returns the quotient and the remainder of this and b.
	// The quotient will be an Int32, Int64 or BigInt.
	QuoRem(b Number) (quotient Number, remainder Number)
}

Number is a general numeric type.

func AsNumber

func AsNumber(a interface{}) Number

AsNumber converts a numeric value into a Number. The numeric value may be int32, int64, int, float32, float64 or *big.Int. For Int32, Int64, Float64 and *BigInt, it behaves as an identity function. For the other types, it returns nil.

Example
a := AsNumber(2147483647)
fmt.Printf("%T %s\n", a, a.String())
a = AsNumber(2147483648)
fmt.Printf("%T %s\n", a, a.String())
a = AsNumber(-2147483648)
fmt.Printf("%T %s\n", a, a.String())
a = AsNumber(-2147483649)
fmt.Printf("%T %s\n", a, a.String())
Output:

goarith.Int32 2147483647
goarith.Int64 2147483648
goarith.Int32 -2147483648
goarith.Int64 -2147483649

Jump to

Keyboard shortcuts

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