gxbig

package
v1.11.12 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2021 License: Apache-2.0 Imports: 8 Imported by: 14

Documentation

Index

Constants

View Source
const (
	DivFracIncr = 4

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

constant values.

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 Integer added in v1.4.0

type Integer struct {

	// used in hessian encoding/decoding
	// You Should not use it in go
	Signum int32
	Mag    []int

	// Deprecated: compatible with java8 serialize
	FirstNonzeroIntNum int
	LowestSetBit       int
	BitLength          int
	BitCount           int
	// contains filtered or unexported fields
}

Integer represents a integer value.

func (*Integer) Format added in v1.4.0

func (i *Integer) Format(s fmt.State, ch rune)

func (*Integer) FromSignAndMag added in v1.4.0

func (i *Integer) FromSignAndMag(signum int32, mag []int)

FromSignAndMag set data from a array of big-endian unsigned uint32, it's used in hessian decoding @see https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-int-byte:A-

func (*Integer) FromString added in v1.4.0

func (i *Integer) FromString(s string) (err error)

FromString set data from a 10-bases number

func (*Integer) GetSignAndMag added in v1.4.0

func (i *Integer) GetSignAndMag() (signum int32, mag []int)

GetSignAndMag is used in hessian encoding

func (*Integer) GobDecode added in v1.4.0

func (i *Integer) GobDecode(buf []byte) error

func (*Integer) GobEncode added in v1.4.0

func (i *Integer) GobEncode() ([]byte, error)

func (Integer) JavaClassName added in v1.4.0

func (Integer) JavaClassName() string

func (*Integer) MarshalJSON added in v1.4.0

func (i *Integer) MarshalJSON() ([]byte, error)

func (*Integer) MarshalText added in v1.4.0

func (i *Integer) MarshalText() (text []byte, err error)

func (*Integer) SetValue added in v1.4.0

func (i *Integer) SetValue(value *big.Int)

func (*Integer) String added in v1.4.0

func (i *Integer) String() string

func (*Integer) UnmarshalJSON added in v1.4.0

func (i *Integer) UnmarshalJSON(text []byte) error

func (*Integer) UnmarshalText added in v1.4.0

func (i *Integer) UnmarshalText(text []byte) error

func (*Integer) Value added in v1.4.0

func (i *Integer) Value() *big.Int

type RoundMode

type RoundMode int32

RoundMode is the type for round mode.

Jump to

Keyboard shortcuts

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