Documentation ¶
Index ¶
- Constants
- Variables
- func DecimalAdd(from1, from2, to *Decimal) error
- func DecimalDiv(from1, from2, to *Decimal, fracIncr int) error
- func DecimalMod(from1, from2, to *Decimal) error
- func DecimalMul(from1, from2, to *Decimal) error
- func DecimalPeak(b []byte) (int, error)
- func DecimalSub(from1, from2, to *Decimal) error
- func GetMaxFloat(flen int, decimal int) float64
- func Round(f float64, dec int) float64
- func RoundFloat(f float64) float64
- func Truncate(f float64, dec int) float64
- func TruncateFloat(f float64, flen int, decimal int) (float64, error)
- type Decimal
- func (d *Decimal) Compare(to *Decimal) int
- func (d *Decimal) FromBin(bin []byte, precision, frac int) (binSize int, err error)
- func (d *Decimal) FromBytes(str []byte) error
- func (d *Decimal) FromFloat64(f float64) error
- func (d *Decimal) FromInt(val int64) *Decimal
- func (d *Decimal) FromString(str string) error
- func (d *Decimal) FromUint(val uint64) *Decimal
- func (d *Decimal) GetDigitsFrac() int8
- func (d *Decimal) IsNegative() bool
- func (d *Decimal) IsZero() bool
- func (Decimal) JavaClassName() string
- func (d *Decimal) PrecisionAndFrac() (precision, frac int)
- func (d *Decimal) Round(to *Decimal, frac int, roundMode RoundMode) (err error)
- func (d *Decimal) Shift(shift int) error
- func (d *Decimal) String() string
- func (d *Decimal) ToBin(precision, frac int) ([]byte, error)
- func (d *Decimal) ToBytes() (str []byte)
- func (d *Decimal) ToFloat64() (float64, error)
- func (d *Decimal) ToHashKey() ([]byte, error)
- func (d *Decimal) ToInt() (int64, error)
- func (d *Decimal) ToUint() (uint64, error)
- type Integer
- func (i *Integer) Format(s fmt.State, ch rune)
- func (i *Integer) FromSignAndMag(signum int32, mag []int)
- func (i *Integer) FromString(s string) (err error)
- func (i *Integer) GetSignAndMag() (signum int32, mag []int)
- func (i *Integer) GobDecode(buf []byte) error
- func (i *Integer) GobEncode() ([]byte, error)
- func (Integer) JavaClassName() string
- func (i *Integer) MarshalJSON() ([]byte, error)
- func (i *Integer) MarshalText() (text []byte, err error)
- func (i *Integer) SetValue(value *big.Int)
- func (i *Integer) String() string
- func (i *Integer) UnmarshalJSON(text []byte) error
- func (i *Integer) UnmarshalText(text []byte) error
- func (i *Integer) Value() *big.Int
- type RoundMode
Constants ¶
const BigDecimalStructSize = 40
BigDecimalStructSize is the struct size of Decimal.
Variables ¶
Functions ¶
func DecimalAdd ¶
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 ¶
DecimalDiv does division of two decimals.
from1 - dividend from2 - divisor to - quotient fracIncr - increment of fraction
func DecimalMod ¶
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 ¶
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 ¶
DecimalPeak returns the length of the encoded decimal.
func DecimalSub ¶
DecimalSub subs one decimal from another, sets the result to 'to'.
func GetMaxFloat ¶
GetMaxFloat gets the max float for given flen and decimal.
func Round ¶
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 ¶
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.
Types ¶
type Decimal ¶
type Decimal struct { // for hessian Value string // contains filtered or unexported fields }
Decimal represents a decimal value.
func NewDecFromFloatForTest ¶
NewDecFromFloatForTest creates a Decimal from float, as it returns no error, it should only be used in test.
func NewDecFromStringForTest ¶
NewDecFromStringForTest creates a Decimal from string, as it returns no error, it should only be used in test.
func NewDecFromUint ¶
NewDecFromUint creates a Decimal from uint.
func NewMaxOrMinDec ¶
NewMaxOrMinDec returns the max or min value decimal for given precision and fraction.
func (*Decimal) FromFloat64 ¶
FromFloat64 creates a decimal from float64 value.
func (*Decimal) FromString ¶
FromBytes parses decimal from string.
func (*Decimal) GetDigitsFrac ¶
GetDigitsFrac returns the digitsFrac.
func (*Decimal) IsNegative ¶
IsNegative returns whether a decimal is negative.
func (Decimal) JavaClassName ¶
func (*Decimal) PrecisionAndFrac ¶
PrecisionAndFrac returns the internal precision and frac number.
func (*Decimal) Round ¶
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 ¶
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) ToBin ¶
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 ¶
ToString converts decimal to its printable string representation without rounding.
RETURN VALUE str - result string errCode - eDecOK/eDecTruncate/eDecOverflow
func (*Decimal) ToHashKey ¶
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.
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) FromSignAndMag ¶ added in v1.4.0
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
FromString set data from a 10-bases number
func (*Integer) GetSignAndMag ¶ added in v1.4.0
GetSignAndMag is used in hessian encoding