numeric

package module
v0.0.0-...-986dec2 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2015 License: GPL-2.0 Imports: 6 Imported by: 0

README

numeric

The numeric package for go adds numeric types that aid with number handling.

GoDoc

Documentation

Overview

Package numeric contains structs and interfaces that represent numeric types. It creates wrappers around numeric types, giving them a common interface that can be used to handle numbers (see Numeric interface) and a type that provides user-friendly interaction (see Number type). This package is not intended to replace the builtin types (int, float64, ...), but rather to make number manipulation easier in cases where it's necessary to support a wide range of approximated numbers without the performance penalty of always using an arbitrary precision type (big.Int and big.Rat). It's important to note that since one of the Numeric types is Float, calculations that require exact numbers or decimal precision should not use this package. All types in this package are immutable.

Numeric interface

The default implementations of the Numeric interface (see Float and BigFloat) are aliases to the builtin types (float64 and big.Rat) and, therefore, one may obtain a Numeric value by simply casting to the Numeric type:

x := 15.0 // float64
y := numeric.Float(x) // numeric.Float
fmt.Println(y) // => 15

However, Numeric types are merely wrappers around their builtin counterparts and don't provide support for automatic conversion between Numeric types, e.g., when a Float value reaches infinity and needs to be converted to a BigFloat.

Number type

In order to get automatic promotion from a smaller type (e.g. Float) to a larger type (e.g. BigFloat), the Number type should be used.

Usage

A value of Number type can be created through the NewNumber() and NewNumberSafe() methods. Both methods accept an arbitrary type (interface{}), and attempt to convert it to a Number value. If such a conversion does not exist, NewNumberSafe() will return an error and NewNumber() will panic.

x, err := numeric.NewNumberSafe("string")
if err != nil {
  handleError(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BigFloat

type BigFloat big.Rat

BigFloat is an implementation of the Numeric interface for the big.Rat type. It cannot be promoted.

func (*BigFloat) Add

func (n *BigFloat) Add(rhs Numeric) Numeric

Add returns a *BigFloat that is the sum of n and rhs.

func (*BigFloat) BigRat

func (n *BigFloat) BigRat() (*big.Rat, error)

BigRat returns a copy of the underlying value of this BigFloat.

func (*BigFloat) CompareTo

func (n *BigFloat) CompareTo(rhs Numeric) int

CompareTo compares n to rhs and returns 1 if n is greater than rhs, -1 if it's smaller than rhs and 0 if the two values are equivalent.

func (*BigFloat) Divide

func (n *BigFloat) Divide(rhs Numeric) Numeric

Divide returns a *BigFloat that is the quotient of n and rhs.

func (*BigFloat) Float64

func (n *BigFloat) Float64() (float64, error)

Float64 converts this BigFloat to a float64 value and returns it. If the value is too large or too small, an error will be returned and the result of the conversion will be +/- infinity, respectively.

func (*BigFloat) Multiply

func (n *BigFloat) Multiply(rhs Numeric) Numeric

Multiply returns a *BigFloat that is the product of n and rhs.

func (*BigFloat) Negate

func (n *BigFloat) Negate() Numeric

Negate returns a *BigFloat that is the negative of n.

func (*BigFloat) New

func (n *BigFloat) New(value int64) Numeric

New returns a *BigFloat equivalent to value.

func (*BigFloat) Promote

func (n *BigFloat) Promote() Numeric

Promote returns a copy of n.

func (*BigFloat) ShouldPromote

func (n *BigFloat) ShouldPromote() bool

ShouldPromote returns false for *BigFloat.

func (*BigFloat) String

func (n *BigFloat) String() string

String returns a string representation of this BigFloat.

func (*BigFloat) Subtract

func (n *BigFloat) Subtract(rhs Numeric) Numeric

Subtract returns a *BigFloat that is the difference of n and rhs.

type Float

type Float float64

Float is an implementation of the Numeric interface for the float64 type. It can be promoted to a BigFloat and will return true on ShouldPromote() if it's equal to +/-Infinity or NaN.

func (Float) Add

func (n Float) Add(rhs Numeric) Numeric

Add returns a Float that is the sum of n and rhs.

func (Float) BigRat

func (n Float) BigRat() (*big.Rat, error)

BigRat converts this float to a big.Rat value and returns it. If the value is +/-Infinity or NaN, an error will be returned. For +Infinity, the result will be set to math.MaxFloat64 + math.SmallestNonzeroFloat64 and for -Infinity, the negative of that will be returned. If n is NaN, the result will be 0.

func (Float) CompareTo

func (n Float) CompareTo(rhs Numeric) int

CompareTo compares n to rhs and returns 1 if n is greater than rhs, -1 if it's smaller than rhs and 0 if the two values are equivalent.

func (Float) Divide

func (n Float) Divide(rhs Numeric) Numeric

Divide returns a Float that is the quotient of n and rhs.

func (Float) Float64

func (n Float) Float64() (float64, error)

Float64 returns a copy of the underlying value of this Float.

func (Float) Multiply

func (n Float) Multiply(rhs Numeric) Numeric

Multiply returns a Float that is the product of n and rhs.

func (Float) Negate

func (n Float) Negate() Numeric

Negate returns a Float that is the negative of n.

func (Float) New

func (n Float) New(value int64) Numeric

New returns a Float equivalent to value.

func (Float) Promote

func (n Float) Promote() Numeric

Promote returns a BigFloat with the same value as n.

func (Float) ShouldPromote

func (n Float) ShouldPromote() bool

ShouldPromote returns true only if n is +/-Infinity or NaN.

func (Float) String

func (n Float) String() string

String returns a string representation of this Float.

func (Float) Subtract

func (n Float) Subtract(rhs Numeric) Numeric

Subtract returns a Float that is the difference of n and rhs.

type Number

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

Number stores a numeric value using a Numeric type as its underlying storage. It can be created through the NewNumber and NewNumberSafe methods.

func NewNumber

func NewNumber(value interface{}) Number

NewNumber attempts to convert a value to a Number and panics if it fails.

func NewNumberSafe

func NewNumberSafe(value interface{}) (number Number, err error)

NewNumberSafe attempts to convert a value to a Number and returns an error if it fails.

func (Number) Add

func (n Number) Add(rhs Number) Number

Add returns a Number that is the sum of n and rhs and automatically promotes it if required.

func (Number) BigInt

func (n Number) BigInt() *big.Int

BigInt returns a *big.Int with the value of n.

func (Number) BigRat

func (n Number) BigRat() *big.Rat

BigRat returns a big.Rat with the value of n.

func (Number) CompareTo

func (n Number) CompareTo(rhs Number) int

CompareTo compares n to rhs and returns 1 if n is greater than rhs, -1 if it's smaller than rhs and 0 if the two values are equivalent.

func (Number) Divide

func (n Number) Divide(rhs Number) Number

Divide returns a Number that is the quotient of n and rhs and automatically promotes it if required.

func (Number) Equals

func (n Number) Equals(rhs Number) bool

Equals compares n to rhs and returns 1 if n is greater than rhs, -1 if it's smaller than rhs and 0 if the two values are equivalent.

func (Number) Float64

func (n Number) Float64() float64

Float64 returns a float64 with the value of n. If the value is invalid, too large or too small, the values NaN and +/-Infinity may be returned.

func (Number) Int64

func (n Number) Int64() (int64, error)

Int64 returns an int64 with the value of n. If n cannot be represented as an int64, an error will be returned and the result of Int64() will be a sensible attempt to represent n.

func (Number) Multiply

func (n Number) Multiply(rhs Number) Number

Multiply returns a Number that is the product of n and rhs and automatically promotes it if required.

func (Number) Negative

func (n Number) Negative() Number

Negative returns a Number that is the negative of n and automatically promotes it if required.

func (Number) String

func (n Number) String() string

String returns a string representation of n.

func (Number) Subtract

func (n Number) Subtract(rhs Number) Number

Subtract returns a Number that is the difference of n and rhs and automatically promotes it if required.

type Numeric

type Numeric interface {
	New(value int64) Numeric
	Negate() Numeric
	Add(rhs Numeric) Numeric
	Subtract(rhs Numeric) Numeric
	Multiply(rhs Numeric) Numeric
	Divide(rhs Numeric) Numeric
	Float64() (float64, error)
	BigRat() (*big.Rat, error)
	CompareTo(rhs Numeric) int
	ShouldPromote() bool
	Promote() Numeric
	String() string
}

Numeric is an interface that represents any numeric type, such as float64. Default implementations exist for the types float64 (type Float) and big.Rat(type BigFloat).

Method New() should return the value provided. The Negate(), Add(), Subtract(), Multiply() and Divide() methods are aliases for the corresponding operations. To compare two Numeric values, the method CompareTo() should be called. For inputs x and y, it should return:

x.CompareTo(y)
	// 0 if x == y
	// 1 if x > y
	// -1 if x < y

The available conversion methods are Float64() and BigRat(). Both of these methods return the result and an error, which should represent overflow or invalid conversion errors. If an error occurs, the methods should attempt to return a sensible value, but may fallback to the default zero value. Numeric also supports string conversion through the Stringer interface.

Promote() and ShouldPromote() are part of a value promotion system. If a value is to big or small to be adequately represented in its Numeric type, it can be promoted (converted) to a type that has a larger capacity. If this happens, the method ShouldPromote() should return true and the method Promote() should return the promoted value.

Jump to

Keyboard shortcuts

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