decimal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2019 License: Apache-2.0 Imports: 8 Imported by: 3

README

⛔ NOTICE: USE OF THIS LIBRARY IN ANZ SYSTEMS IS STRICTLY PROHIBITED. ⛔
This library is currently in development and has not undergone a thorough correctness assessment or security audit. It is therefore NOT suitable for production use until full approval is granted by the ANZ open source committee and this notice removed.

decimal

This library implements fixed-precision decimal numbers based on IEEE 754R standard; https://ieeexplore.ieee.org/document/4674342 More info can be found at: http://speleotrove.com/decimal/

Features

  • Decimal64, partial implementation of the ieee-754R standard
  • Half up and half even rounding
  • Up to 3 times faster than arbitrary precision decimal libraries in Go

Goals

  • To implement 128 bit decimal

Installation and use

Run go get github.com/anz-bank/decimal

package main

import (
	"fmt"

	"github.com/anz-bank/decimal"
)

func main() {
	var a decimal.Decimal64
	b := decimal.MustParse64("0.1")
	c := decimal.MustParse64("0.3")
	d := decimal.New64FromInt64(123456)

	fmt.Println(a, b, c, d)
}

Docs

https://godoc.org/github.com/anz-bank/decimal

Why decimal

Binary floating point numbers are fundamentally flawed when it comes to representing exact numbers in a decimal world. Just like 1/3 can't be represented in base 10 (it evaluates to 0.3333333333 repeating), 1/10 can't be represented in binary. The solution is to use a decimal floating point number. Binary floating point numbers (often just called floating point numbers) are usually in the form Sign * Significand * 2 ^ exp and decimal floating point numbers change this to Sign * Significand * 10 ^ exp This eliminates the decimal fraction problem, as the base is in 10.

Why fixed precision

Most implementations of a decimal floating point datatype implement an 'arbitrary precision' type, which often uses an underlying big int. This gives flexibility in that as the number grows, the number of bits assigned to the number grows ( and thus 'arbitrary precision'). This library is different as it specifies a 64 bit decimal datatype as specified in the ieee-754R standard. This gives the sacrifice of being able to represent arbitrarily large numbers, but is faster than other arbitrary precision libraries.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultContext = Context64{roundHalfUp}

DefaultContext is the context that Arithmetic functions will use in order to do calculations

View Source
var E64 = newFromParts(0, -15, 2718281828459045)

E64 represents e (lim[n→∞](1+1/n)ⁿ).

View Source
var Infinity64 = Decimal64{inf64}

Infinity64 represents ∞ as a Decimal64.

View Source
var Max64 = newFromParts(0, expMax, maxSig)

Max64 is the maximum number representable with a Decimal64.

View Source
var Min64 = newFromParts(0, -398, 1)

Min64 is the smallest number that is subnormal possible with Decimal64.

View Source
var NegInfinity64 = Decimal64{neg64 | inf64}

NegInfinity64 represents -∞ as a Decimal64.

View Source
var NegMax64 = newFromParts(1, expMax, maxSig)

NegMax64 is the minimum finite number (most negative) possible with Decimal64 (negative).

View Source
var NegOne64 = newFromParts(1, -15, decimal64Base)

NegOne64 represents -1 as a Decimal64.

View Source
var NegZero64 = newFromParts(1, 0, 0)

NegZero64 represents -0 as a Decimal64.

View Source
var One64 = newFromParts(0, -15, decimal64Base)

One64 represents 1 as a Decimal64.

View Source
var Pi64 = newFromParts(0, -15, 3141592653589793)

Pi64 represents π.

View Source
var QNaN64 = Decimal64{0x7c << 56}

QNaN64 represents a quiet NaN as a Decimal64.

View Source
var SNaN64 = Decimal64{0x7e << 56}

SNaN64 represents a signalling NaN as a Decimal64.

View Source
var Zero64 = newFromParts(0, 0, 0)

Zero64 represents 0 as a Decimal64.

Functions

This section is empty.

Types

type Context64

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

Context64 stores the rounding type for arithmetic operations.

func (Context64) Add

func (ctx Context64) Add(d, e Decimal64) Decimal64

Add computes d + e

func (Context64) FMA

func (ctx Context64) FMA(d, e, f Decimal64) Decimal64

FMA computes d*e + f

func (Context64) Mul

func (ctx Context64) Mul(d, e Decimal64) Decimal64

Mul computes d * e

func (Context64) Quo

func (ctx Context64) Quo(d, e Decimal64) Decimal64

Quo computes d / e.

type Decimal64

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

Decimal64 represents an IEEE 754 64-bit floating point decimal number. It uses the binary representation method. Decimal64 is intentionally a struct to ensure users don't accidentally cast it to uint64

func MustParse64

func MustParse64(s string) Decimal64

MustParse64 parses a string as a Decimal64 and returns the value or panics if the string doesn't represent a valid Decimal64.

func New64FromInt64

func New64FromInt64(value int64) Decimal64

New64FromInt64 returns a new Decimal64 with the given value.

func Parse64

func Parse64(s string) (Decimal64, error)

Parse64 parses a string representation of a number as a Decimal64.

func (Decimal64) Abs

func (d Decimal64) Abs() Decimal64

Abs computes ||d||.

func (Decimal64) Add

func (d Decimal64) Add(e Decimal64) Decimal64

Add computes d + e with default rounding

func (Decimal64) Append

func (d Decimal64) Append(buf []byte, format byte, prec int) []byte

Append appends the text representation of d to buf.

func (Decimal64) Class

func (d Decimal64) Class() string

Class returns a string of the 'type' that the decimal is.

func (Decimal64) Cmp

func (d Decimal64) Cmp(e Decimal64) int

Cmp returns:

-2 if d or e is NaN
-1 if d <  e
 0 if d == e (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf)
+1 if d >  e

func (Decimal64) FMA

func (d Decimal64) FMA(e, f Decimal64) Decimal64

FMA computes d*e + f with default rounding.

func (Decimal64) Float64

func (d Decimal64) Float64() float64

Float64 returns a float64 representation of d.

func (Decimal64) Format

func (d Decimal64) Format(s fmt.State, format rune)

Format implements fmt.Formatter.

func (*Decimal64) GobDecode

func (d *Decimal64) GobDecode(buf []byte) error

GobDecode implements encoding.GobDecoder.

func (Decimal64) GobEncode

func (d Decimal64) GobEncode() ([]byte, error)

GobEncode implements encoding.GobEncoder.

func (Decimal64) Int64

func (d Decimal64) Int64() int64

Int64 converts d to an int64.

func (Decimal64) IsInf

func (d Decimal64) IsInf() bool

IsInf returns true iff d = ±∞.

func (Decimal64) IsInt

func (d Decimal64) IsInt() bool

IsInt returns true iff d is an integer.

func (Decimal64) IsNaN

func (d Decimal64) IsNaN() bool

IsNaN returns true iff d is not a number.

func (Decimal64) IsQNaN

func (d Decimal64) IsQNaN() bool

IsQNaN returns true iff d is a quiet NaN.

func (Decimal64) IsSNaN

func (d Decimal64) IsSNaN() bool

IsSNaN returns true iff d is a signalling NaN.

func (Decimal64) IsSubnormal

func (d Decimal64) IsSubnormal() bool

IsSubnormal returns true iff d is a subnormal.

func (Decimal64) IsZero

func (d Decimal64) IsZero() bool

IsZero returns true if the Decimal encodes a zero value.

func (Decimal64) MarshalJSON

func (d Decimal64) MarshalJSON() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Decimal64) MarshalText

func (d Decimal64) MarshalText() []byte

MarshalText implements the encoding.TextMarshaler interface.

func (Decimal64) Mul

func (d Decimal64) Mul(e Decimal64) Decimal64

Mul computes d * e with default rounding.

func (Decimal64) Neg

func (d Decimal64) Neg() Decimal64

Neg computes -d.

func (Decimal64) Quo

func (d Decimal64) Quo(e Decimal64) Decimal64

Quo computes d / e with default rounding.

func (*Decimal64) Scan

func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error

Scan implements fmt.Scanner.

func (Decimal64) Sign

func (d Decimal64) Sign() int

Sign returns -1/0/1 depending on whether d is </=/> 0.

func (Decimal64) Signbit

func (d Decimal64) Signbit() bool

Signbit returns true iff d is negative or -0.

func (Decimal64) Sqrt

func (d Decimal64) Sqrt() Decimal64

Sqrt computes √d.

func (Decimal64) String

func (d Decimal64) String() string

String returns a string representation of d.

func (Decimal64) Sub

func (d Decimal64) Sub(e Decimal64) Decimal64

Sub returns d - e.

func (Decimal64) Text

func (d Decimal64) Text(format byte, prec int) string

Text converts the floating-point number x to a string according to the given format and precision prec.

func (*Decimal64) UnmarshalJSON

func (d *Decimal64) UnmarshalJSON(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (*Decimal64) UnmarshalText

func (d *Decimal64) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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