gxbig

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const BigDecimalStructSize = 40

BigDecimalStructSize is the struct size of Decimal.

Variables

View Source
var (
	ErrBadNumber = errors.Errorf("Bad Number")
	ErrOverflow  = errors.Errorf("Data Overflow")
	ErrTruncated = errors.Errorf("Data Truncated")
	ErrDivByZero = errors.Errorf("Division by 0")
)

Functions

func DecimalAdd

func DecimalAdd(from1, from2, to *Decimal) error

DecimalAdd adds two decimals, sets the result to 'to'. Note: DO NOT use `from1` or `from2` as `to` since the metadata of `to` may be changed during evaluating.

func DecimalDiv

func DecimalDiv(from1, from2, to *Decimal, fracIncr int) error

DecimalDiv does division of two decimals.

from1 - dividend from2 - divisor to - quotient fracIncr - increment of fraction

func DecimalMod

func DecimalMod(from1, from2, to *Decimal) error

DecimalMod does modulus of two decimals.

    from1   - dividend
    from2   - divisor
    to      - modulus

RETURN VALUE
  E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;

NOTES
  see do_div_mod()

DESCRIPTION
  the modulus R in    R = M mod N

 is defined as

   0 <= |R| < |M|
   sign R == sign M
   R = M - k*N, where k is integer

 thus, there's no requirement for M or N to be integers

func DecimalMul

func DecimalMul(from1, from2, to *Decimal) error

DecimalMul multiplies two decimals.

    from1, from2 - factors
    to      - product

RETURN VALUE
  E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;

NOTES
  in this implementation, with wordSize=4 we have digitsPerWord=9,
  and 63-digit number will take only 7 words (basically a 7-digit
  "base 999999999" number).  Thus there's no need in fast multiplication
  algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
  method.

  XXX if this library is to be used with huge numbers of thousands of
  digits, fast multiplication must be implemented.

func DecimalPeak

func DecimalPeak(b []byte) (int, error)

DecimalPeak returns the length of the encoded decimal.

func DecimalSub

func DecimalSub(from1, from2, to *Decimal) error

DecimalSub subs one decimal from another, sets the result to 'to'.

func GetMaxFloat

func GetMaxFloat(flen int, decimal int) float64

GetMaxFloat gets the max float for given flen and decimal.

func Round

func Round(f float64, dec int) float64

Round rounds the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero.

func RoundFloat

func RoundFloat(f float64) float64

RoundFloat rounds float val to the nearest integer value with float64 format, like MySQL Round function. RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html so rounding use "round half away from zero". e.g, 1.5 -> 2, -1.5 -> -2.

func Truncate

func Truncate(f float64, dec int) float64

Truncate truncates the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero.

func TruncateFloat

func TruncateFloat(f float64, flen int, decimal int) (float64, error)

TruncateFloat tries to truncate f. If the result exceeds the max/min float that flen/decimal allowed, returns the max/min float allowed.

Types

type Decimal

type Decimal struct {

	// for hessian
	Value string
	// contains filtered or unexported fields
}

Decimal represents a decimal value.

func DecimalNeg

func DecimalNeg(from *Decimal) *Decimal

DecimalNeg reverses decimal's sign.

func NewDecFromFloatForTest

func NewDecFromFloatForTest(f float64) *Decimal

NewDecFromFloatForTest creates a Decimal from float, as it returns no error, it should only be used in test.

func NewDecFromInt

func NewDecFromInt(i int64) *Decimal

NewDecFromInt creates a Decimal from int.

func NewDecFromStringForTest

func NewDecFromStringForTest(s string) *Decimal

NewDecFromStringForTest creates a Decimal from string, as it returns no error, it should only be used in test.

func NewDecFromUint

func NewDecFromUint(i uint64) *Decimal

NewDecFromUint creates a Decimal from uint.

func NewMaxOrMinDec

func NewMaxOrMinDec(negative bool, prec, frac int) *Decimal

NewMaxOrMinDec returns the max or min value decimal for given precision and fraction.

func (*Decimal) Compare

func (d *Decimal) Compare(to *Decimal) int

Compare compares one decimal to another, returns -1/0/1.

func (*Decimal) FromBin

func (d *Decimal) FromBin(bin []byte, precision, frac int) (binSize int, err error)

FromBin Restores decimal from its binary fixed-length representation.

func (*Decimal) FromBytes

func (d *Decimal) FromBytes(str []byte) error

FromBytes parses decimal from bytes.

func (*Decimal) FromFloat64

func (d *Decimal) FromFloat64(f float64) error

FromFloat64 creates a decimal from float64 value.

func (*Decimal) FromInt

func (d *Decimal) FromInt(val int64) *Decimal

FromInt sets the decimal value from int64.

func (*Decimal) FromString

func (d *Decimal) FromString(str string) error

FromBytes parses decimal from string.

func (*Decimal) FromUint

func (d *Decimal) FromUint(val uint64) *Decimal

FromUint sets the decimal value from uint64.

func (*Decimal) GetDigitsFrac

func (d *Decimal) GetDigitsFrac() int8

GetDigitsFrac returns the digitsFrac.

func (*Decimal) IsNegative

func (d *Decimal) IsNegative() bool

IsNegative returns whether a decimal is negative.

func (*Decimal) IsZero

func (d *Decimal) IsZero() bool

IsZero checks whether it's a zero decimal.

func (Decimal) JavaClassName

func (Decimal) JavaClassName() string

func (*Decimal) PrecisionAndFrac

func (d *Decimal) PrecisionAndFrac() (precision, frac int)

PrecisionAndFrac returns the internal precision and frac number.

func (*Decimal) Round

func (d *Decimal) Round(to *Decimal, frac int, roundMode RoundMode) (err error)

Round rounds the decimal to "frac" digits.

   to			- result buffer. d == to is allowed
   frac			- to what position after fraction point to round. can be negative!
   roundMode		- round to nearest even or truncate
			ModeHalfEven rounds normally.
			Truncate just truncates the decimal.

NOTES

scale can be negative !
one TRUNCATED error (line XXX below) isn't treated very logical :(

RETURN VALUE

eDecOK/eDecTruncated

func (*Decimal) Shift

func (d *Decimal) Shift(shift int) error

Shift shifts decimal digits in given number (with rounding if it need), shift > 0 means shift to left shift, shift < 0 means right shift. In fact it is multiplying on 10^shift.

RETURN

eDecOK          OK
eDecOverflow    operation lead to overflow, number is untoched
eDecTruncated   number was rounded to fit into buffer

func (*Decimal) String

func (d *Decimal) String() string

String returns the decimal string representation rounded to resultFrac.

func (*Decimal) ToBin

func (d *Decimal) ToBin(precision, frac int) ([]byte, error)

ToBin converts decimal to its binary fixed-length representation two representations of the same length can be compared with memcmp with the correct -1/0/+1 result

  PARAMS
		precision/frac - if precision is 0, internal value of the decimal will be used,
		then the encoded value is not memory comparable.

  NOTE
    the buffer is assumed to be of the size decimalBinSize(precision, frac)

  RETURN VALUE
  	bin     - binary value
    errCode - eDecOK/eDecTruncate/eDecOverflow

  DESCRIPTION
    for storage decimal numbers are converted to the "binary" format.

    This format has the following properties:
      1. length of the binary representation depends on the {precision, frac}
      as provided by the caller and NOT on the digitsInt/digitsFrac of the decimal to
      convert.
      2. binary representations of the same {precision, frac} can be compared
      with memcmp - with the same result as DecimalCompare() of the original
      decimals (not taking into account possible precision loss during
      conversion).

    This binary format is as follows:
      1. First the number is converted to have a requested precision and frac.
      2. Every full digitsPerWord digits of digitsInt part are stored in 4 bytes
         as is
      3. The first digitsInt % digitesPerWord digits are stored in the reduced
         number of bytes (enough bytes to store this number of digits -
         see dig2bytes)
      4. same for frac - full word are stored as is,
         the last frac % digitsPerWord digits - in the reduced number of bytes.
      5. If the number is negative - every byte is inversed.
      5. The very first bit of the resulting byte array is inverted (because
         memcmp compares unsigned bytes, see property 2 above)

    Example:

      1234567890.1234

    internally is represented as 3 words

      1 234567890 123400000

    (assuming we want a binary representation with precision=14, frac=4)
    in hex it's

      00-00-00-01  0D-FB-38-D2  07-5A-EF-40

    now, middle word is full - it stores 9 decimal digits. It goes
    into binary representation as is:

      ...........  0D-FB-38-D2 ............

    First word has only one decimal digit. We can store one digit in
    one byte, no need to waste four:

                01 0D-FB-38-D2 ............

    now, last word. It's 123400000. We can store 1234 in two bytes:

                01 0D-FB-38-D2 04-D2

    So, we've packed 12 bytes number in 7 bytes.
    And now we invert the highest bit to get the final result:

                81 0D FB 38 D2 04 D2

    And for -1234567890.1234 it would be

                7E F2 04 C7 2D FB 2D

func (*Decimal) ToBytes

func (d *Decimal) ToBytes() (str []byte)

ToString converts decimal to its printable string representation without rounding.

RETURN VALUE

    str       - result string
    errCode   - eDecOK/eDecTruncate/eDecOverflow

func (*Decimal) ToFloat64

func (d *Decimal) ToFloat64() (float64, error)

ToFloat64 converts decimal to float64 value.

func (*Decimal) ToHashKey

func (d *Decimal) ToHashKey() ([]byte, error)

ToHashKey removes the leading and trailing zeros and generates a hash key. Two Decimals dec0 and dec1 with different fraction will generate the same hash keys if dec0.Compare(dec1) == 0.

func (*Decimal) ToInt

func (d *Decimal) ToInt() (int64, error)

ToInt returns int part of the decimal, returns the result and errcode.

func (*Decimal) ToUint

func (d *Decimal) ToUint() (uint64, error)

ToUint returns int part of the decimal, returns the result and errcode.

type RoundMode

type RoundMode int32

RoundMode is the type for round mode.

const (
	DivFracIncr = 4

	// ModeHalfEven rounds normally.
	ModeHalfEven RoundMode = 5
	// Truncate just truncates the decimal.
	ModeTruncate RoundMode = 10
)

constant values.

Jump to

Keyboard shortcuts

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