github.com/apache/arrow/go/v16@v16.1.0/arrow/decimal256/decimal256.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package decimal256
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"math"
    23  	"math/big"
    24  	"math/bits"
    25  
    26  	"github.com/apache/arrow/go/v16/arrow/decimal128"
    27  	"github.com/apache/arrow/go/v16/arrow/internal/debug"
    28  )
    29  
    30  const (
    31  	MaxPrecision = 76
    32  	MaxScale     = 76
    33  )
    34  
    35  func GetMaxValue(prec int32) Num {
    36  	return scaleMultipliers[prec].Sub(FromU64(1))
    37  }
    38  
    39  type Num struct {
    40  	// arr[0] is the lowest bits, arr[3] is the highest bits
    41  	arr [4]uint64
    42  }
    43  
    44  // New returns a new signed 256-bit integer value where x1 contains
    45  // the highest bits with the rest of the values in order down to the
    46  // lowest bits
    47  //
    48  //	ie: New(1, 2, 3, 4) returns with the elements in little-endian order
    49  //	    {4, 3, 2, 1} but each value is still represented as the native endianness
    50  func New(x1, x2, x3, x4 uint64) Num {
    51  	return Num{[4]uint64{x4, x3, x2, x1}}
    52  }
    53  
    54  func (n Num) Array() [4]uint64 { return n.arr }
    55  
    56  func (n Num) LowBits() uint64 { return n.arr[0] }
    57  
    58  func FromDecimal128(n decimal128.Num) Num {
    59  	var topBits uint64
    60  	if n.Sign() < 0 {
    61  		topBits = math.MaxUint64
    62  	}
    63  	return New(topBits, topBits, uint64(n.HighBits()), n.LowBits())
    64  }
    65  
    66  func FromU64(v uint64) Num {
    67  	return Num{[4]uint64{v, 0, 0, 0}}
    68  }
    69  
    70  func FromI64(v int64) Num {
    71  	switch {
    72  	case v > 0:
    73  		return New(0, 0, 0, uint64(v))
    74  	case v < 0:
    75  		return New(math.MaxUint64, math.MaxUint64, math.MaxUint64, uint64(v))
    76  	default:
    77  		return Num{}
    78  	}
    79  }
    80  
    81  func (n Num) Negate() Num {
    82  	var carry uint64 = 1
    83  	for i := range n.arr {
    84  		n.arr[i] = ^n.arr[i] + carry
    85  		if n.arr[i] != 0 {
    86  			carry = 0
    87  		}
    88  	}
    89  	return n
    90  }
    91  
    92  func (n Num) Add(rhs Num) Num {
    93  	var carry uint64
    94  	for i, v := range n.arr {
    95  		n.arr[i], carry = bits.Add64(v, rhs.arr[i], carry)
    96  	}
    97  	return n
    98  }
    99  
   100  func (n Num) Sub(rhs Num) Num {
   101  	return n.Add(rhs.Negate())
   102  }
   103  
   104  func (n Num) Mul(rhs Num) Num {
   105  	b := n.BigInt()
   106  	return FromBigInt(b.Mul(b, rhs.BigInt()))
   107  }
   108  
   109  func (n Num) Div(rhs Num) (res, rem Num) {
   110  	b := n.BigInt()
   111  	out, remainder := b.QuoRem(b, rhs.BigInt(), &big.Int{})
   112  	return FromBigInt(out), FromBigInt(remainder)
   113  }
   114  
   115  func (n Num) Pow(rhs Num) Num {
   116  	b := n.BigInt()
   117  	return FromBigInt(b.Exp(b, rhs.BigInt(), nil))
   118  }
   119  
   120  var pt5 = big.NewFloat(0.5)
   121  
   122  func FromString(v string, prec, scale int32) (n Num, err error) {
   123  	// time for some math!
   124  	// Our input precision means "number of digits of precision" but the
   125  	// math/big library refers to precision in floating point terms
   126  	// where it refers to the "number of bits of precision in the mantissa".
   127  	// So we need to figure out how many bits we should use for precision,
   128  	// based on the input precision. Too much precision and we aren't rounding
   129  	// when we should. Too little precision and we round when we shouldn't.
   130  	//
   131  	// In general, the number of decimal digits you get from a given number
   132  	// of bits will be:
   133  	//
   134  	//	digits = log[base 10](2^nbits)
   135  	//
   136  	// it thus follows that:
   137  	//
   138  	//	digits = nbits * log[base 10](2)
   139  	//  nbits = digits / log[base 10](2)
   140  	//
   141  	// So we need to account for our scale since we're going to be multiplying
   142  	// by 10^scale in order to get the integral value we're actually going to use
   143  	// So to get our number of bits we do:
   144  	//
   145  	// 	(prec + scale + 1) / log[base10](2)
   146  	//
   147  	// Finally, we still have a sign bit, so we -1 to account for the sign bit.
   148  	// Aren't floating point numbers fun?
   149  	var precInBits = uint(math.Round(float64(prec+scale+1)/math.Log10(2))) + 1
   150  
   151  	var out *big.Float
   152  	out, _, err = big.ParseFloat(v, 10, 255, big.ToNearestEven)
   153  	if err != nil {
   154  		return
   155  	}
   156  
   157  	if scale < 0 {
   158  		var tmp big.Int
   159  		val, _ := out.Int(&tmp)
   160  		if val.BitLen() > 255 {
   161  			return Num{}, errors.New("bitlen too large for decimal256")
   162  		}
   163  		n = FromBigInt(val)
   164  
   165  		n, _ = n.Div(scaleMultipliers[-scale])
   166  	} else {
   167  		out.Mul(out, (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt())).SetPrec(precInBits)
   168  		// Since we're going to truncate this to get an integer, we need to round
   169  		// the value instead because of edge cases so that we match how other implementations
   170  		// (e.g. C++) handles Decimal values. So if we're negative we'll subtract 0.5 and if
   171  		// we're positive we'll add 0.5.
   172  		if out.Signbit() {
   173  			out.Sub(out, pt5)
   174  		} else {
   175  			out.Add(out, pt5)
   176  		}
   177  
   178  		var tmp big.Int
   179  		val, _ := out.Int(&tmp)
   180  		if val.BitLen() > 255 {
   181  			return Num{}, errors.New("bitlen too large for decimal256")
   182  		}
   183  		n = FromBigInt(val)
   184  	}
   185  	if !n.FitsInPrecision(prec) {
   186  		err = fmt.Errorf("value %v doesn't fit in precision %d", n, prec)
   187  	}
   188  	return
   189  }
   190  
   191  func FromFloat32(v float32, prec, scale int32) (Num, error) {
   192  	debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting to decimal256")
   193  
   194  	if math.IsInf(float64(v), 0) {
   195  		return Num{}, fmt.Errorf("cannot convert %f to decimal256", v)
   196  	}
   197  
   198  	if v < 0 {
   199  		dec, err := fromPositiveFloat32(-v, prec, scale)
   200  		if err != nil {
   201  			return dec, err
   202  		}
   203  		return dec.Negate(), nil
   204  	}
   205  	return fromPositiveFloat32(v, prec, scale)
   206  }
   207  
   208  func FromFloat64(v float64, prec, scale int32) (Num, error) {
   209  	debug.Assert(prec > 0 && prec <= 76, "invalid precision for converting to decimal256")
   210  
   211  	if math.IsInf(v, 0) {
   212  		return Num{}, fmt.Errorf("cannot convert %f to decimal256", v)
   213  	}
   214  
   215  	if v < 0 {
   216  		dec, err := fromPositiveFloat64(-v, prec, scale)
   217  		if err != nil {
   218  			return dec, err
   219  		}
   220  		return dec.Negate(), nil
   221  	}
   222  	return fromPositiveFloat64(v, prec, scale)
   223  }
   224  
   225  // this has to exist despite sharing some code with fromPositiveFloat64
   226  // because if we don't do the casts back to float32 in between each
   227  // step, we end up with a significantly different answer!
   228  // Aren't floating point values so much fun?
   229  //
   230  // example value to use:
   231  //
   232  //	v := float32(1.8446746e+15)
   233  //
   234  // You'll end up with a different values if you do:
   235  //
   236  //	FromFloat64(float64(v), 20, 4)
   237  //
   238  // vs
   239  //
   240  //	FromFloat32(v, 20, 4)
   241  //
   242  // because float64(v) == 1844674629206016 rather than 1844674600000000
   243  func fromPositiveFloat32(v float32, prec, scale int32) (Num, error) {
   244  	val, err := scalePositiveFloat64(float64(v), prec, scale)
   245  	if err != nil {
   246  		return Num{}, err
   247  	}
   248  
   249  	v = float32(val)
   250  	var arr [4]float32
   251  	arr[3] = float32(math.Floor(math.Ldexp(float64(v), -192)))
   252  	v -= float32(math.Ldexp(float64(arr[3]), 192))
   253  	arr[2] = float32(math.Floor(math.Ldexp(float64(v), -128)))
   254  	v -= float32(math.Ldexp(float64(arr[2]), 128))
   255  	arr[1] = float32(math.Floor(math.Ldexp(float64(v), -64)))
   256  	v -= float32(math.Ldexp(float64(arr[1]), 64))
   257  	arr[0] = v
   258  
   259  	debug.Assert(arr[3] >= 0, "bad conversion float64 to decimal256")
   260  	debug.Assert(arr[3] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   261  	debug.Assert(arr[2] >= 0, "bad conversion float64 to decimal256")
   262  	debug.Assert(arr[2] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   263  	debug.Assert(arr[1] >= 0, "bad conversion float64 to decimal256")
   264  	debug.Assert(arr[1] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   265  	debug.Assert(arr[0] >= 0, "bad conversion float64 to decimal256")
   266  	debug.Assert(arr[0] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   267  	return Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]), uint64(arr[3])}}, nil
   268  }
   269  
   270  func scalePositiveFloat64(v float64, prec, scale int32) (float64, error) {
   271  	var pscale float64
   272  	if scale >= -76 && scale <= 76 {
   273  		pscale = float64PowersOfTen[scale+76]
   274  	} else {
   275  		pscale = math.Pow10(int(scale))
   276  	}
   277  
   278  	v *= pscale
   279  	v = math.RoundToEven(v)
   280  	maxabs := float64PowersOfTen[prec+76]
   281  	if v <= -maxabs || v >= maxabs {
   282  		return 0, fmt.Errorf("cannot convert %f to decimal256(precision=%d, scale=%d): overflow",
   283  			v, prec, scale)
   284  	}
   285  	return v, nil
   286  }
   287  
   288  func fromPositiveFloat64(v float64, prec, scale int32) (Num, error) {
   289  	val, err := scalePositiveFloat64(v, prec, scale)
   290  	if err != nil {
   291  		return Num{}, err
   292  	}
   293  
   294  	var arr [4]float64
   295  	arr[3] = math.Floor(math.Ldexp(val, -192))
   296  	val -= math.Ldexp(arr[3], 192)
   297  	arr[2] = math.Floor(math.Ldexp(val, -128))
   298  	val -= math.Ldexp(arr[2], 128)
   299  	arr[1] = math.Floor(math.Ldexp(val, -64))
   300  	val -= math.Ldexp(arr[1], 64)
   301  	arr[0] = val
   302  
   303  	debug.Assert(arr[3] >= 0, "bad conversion float64 to decimal256")
   304  	debug.Assert(arr[3] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   305  	debug.Assert(arr[2] >= 0, "bad conversion float64 to decimal256")
   306  	debug.Assert(arr[2] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   307  	debug.Assert(arr[1] >= 0, "bad conversion float64 to decimal256")
   308  	debug.Assert(arr[1] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   309  	debug.Assert(arr[0] >= 0, "bad conversion float64 to decimal256")
   310  	debug.Assert(arr[0] < 1.8446744073709552e+19, "bad conversion float64 to decimal256") // 2**64
   311  	return Num{[4]uint64{uint64(arr[0]), uint64(arr[1]), uint64(arr[2]), uint64(arr[3])}}, nil
   312  }
   313  
   314  func (n Num) tofloat64Positive(scale int32) float64 {
   315  	const (
   316  		twoTo64  float64 = 1.8446744073709552e+19
   317  		twoTo128 float64 = 3.402823669209385e+38
   318  		twoTo192 float64 = 6.277101735386681e+57
   319  	)
   320  
   321  	x := float64(n.arr[3]) * twoTo192
   322  	x += float64(n.arr[2]) * twoTo128
   323  	x += float64(n.arr[1]) * twoTo64
   324  	x += float64(n.arr[0])
   325  
   326  	if scale >= -76 && scale <= 76 {
   327  		return x * float64PowersOfTen[-scale+76]
   328  	}
   329  
   330  	return x * math.Pow10(-int(scale))
   331  }
   332  
   333  func (n Num) ToFloat32(scale int32) float32 { return float32(n.ToFloat64(scale)) }
   334  
   335  func (n Num) ToFloat64(scale int32) float64 {
   336  	if n.Sign() < 0 {
   337  		return -n.Negate().tofloat64Positive(scale)
   338  	}
   339  	return n.tofloat64Positive(scale)
   340  }
   341  
   342  func (n Num) Sign() int {
   343  	if n == (Num{}) {
   344  		return 0
   345  	}
   346  	return int(1 | (int64(n.arr[3]) >> 63))
   347  }
   348  
   349  func FromBigInt(v *big.Int) (n Num) {
   350  	bitlen := v.BitLen()
   351  	if bitlen > 255 {
   352  		panic("arrow/decimal256: cannot represent value larger than 256bits")
   353  	} else if bitlen == 0 {
   354  		return
   355  	}
   356  
   357  	b := v.Bits()
   358  	for i, bits := range b {
   359  		n.arr[i] = uint64(bits)
   360  	}
   361  	if v.Sign() < 0 {
   362  		return n.Negate()
   363  	}
   364  	return
   365  }
   366  
   367  func toBigIntPositive(n Num) *big.Int {
   368  	return new(big.Int).SetBits([]big.Word{big.Word(n.arr[0]), big.Word(n.arr[1]), big.Word(n.arr[2]), big.Word(n.arr[3])})
   369  }
   370  
   371  func (n Num) BigInt() *big.Int {
   372  	if n.Sign() < 0 {
   373  		b := toBigIntPositive(n.Negate())
   374  		return b.Neg(b)
   375  	}
   376  	return toBigIntPositive(n)
   377  }
   378  
   379  // Greater returns true if the value represented by n is > other
   380  func (n Num) Greater(other Num) bool {
   381  	return other.Less(n)
   382  }
   383  
   384  // GreaterEqual returns true if the value represented by n is >= other
   385  func (n Num) GreaterEqual(other Num) bool {
   386  	return !n.Less(other)
   387  }
   388  
   389  // Less returns true if the value represented by n is < other
   390  func (n Num) Less(other Num) bool {
   391  	switch {
   392  	case n.arr[3] != other.arr[3]:
   393  		return int64(n.arr[3]) < int64(other.arr[3])
   394  	case n.arr[2] != other.arr[2]:
   395  		return n.arr[2] < other.arr[2]
   396  	case n.arr[1] != other.arr[1]:
   397  		return n.arr[1] < other.arr[1]
   398  	}
   399  	return n.arr[0] < other.arr[0]
   400  }
   401  
   402  // LessEqual returns true if the value represented by n is <= other
   403  func (n Num) LessEqual(other Num) bool {
   404  	return !n.Greater(other)
   405  }
   406  
   407  // Max returns the largest Decimal256 that was passed in the arguments
   408  func Max(first Num, rest ...Num) Num {
   409  	answer := first
   410  	for _, number := range rest {
   411  		if number.Greater(answer) {
   412  			answer = number
   413  		}
   414  	}
   415  	return answer
   416  }
   417  
   418  // Min returns the smallest Decimal256 that was passed in the arguments
   419  func Min(first Num, rest ...Num) Num {
   420  	answer := first
   421  	for _, number := range rest {
   422  		if number.Less(answer) {
   423  			answer = number
   424  		}
   425  	}
   426  	return answer
   427  }
   428  
   429  // Cmp compares the numbers represented by n and other and returns:
   430  //
   431  //	+1 if n > other
   432  //	 0 if n == other
   433  //	-1 if n < other
   434  func (n Num) Cmp(other Num) int {
   435  	switch {
   436  	case n.Greater(other):
   437  		return 1
   438  	case n.Less(other):
   439  		return -1
   440  	}
   441  	return 0
   442  }
   443  
   444  func (n Num) IncreaseScaleBy(increase int32) Num {
   445  	debug.Assert(increase >= 0, "invalid amount to increase scale by")
   446  	debug.Assert(increase <= 76, "invalid amount to increase scale by")
   447  
   448  	v := scaleMultipliers[increase].BigInt()
   449  	return FromBigInt(v.Mul(n.BigInt(), v))
   450  }
   451  
   452  func (n Num) ReduceScaleBy(reduce int32, round bool) Num {
   453  	debug.Assert(reduce >= 0, "invalid amount to reduce scale by")
   454  	debug.Assert(reduce <= 76, "invalid amount to reduce scale by")
   455  
   456  	if reduce == 0 {
   457  		return n
   458  	}
   459  
   460  	divisor := scaleMultipliers[reduce].BigInt()
   461  	result, remainder := divisor.QuoRem(n.BigInt(), divisor, new(big.Int))
   462  	if round {
   463  		divisorHalf := scaleMultipliersHalf[reduce]
   464  		if remainder.Abs(remainder).Cmp(divisorHalf.BigInt()) != -1 {
   465  			result.Add(result, big.NewInt(int64(n.Sign())))
   466  		}
   467  	}
   468  	return FromBigInt(result)
   469  }
   470  
   471  func (n Num) rescaleWouldCauseDataLoss(deltaScale int32, multiplier Num) (out Num, loss bool) {
   472  	if deltaScale < 0 {
   473  		var remainder Num
   474  		out, remainder = n.Div(multiplier)
   475  		return out, remainder != Num{}
   476  	}
   477  
   478  	out = n.Mul(multiplier)
   479  	if n.Sign() < 0 {
   480  		loss = n.Less(out)
   481  	} else {
   482  		loss = out.Less(n)
   483  	}
   484  	return
   485  }
   486  
   487  func (n Num) Rescale(original, newscale int32) (out Num, err error) {
   488  	if original == newscale {
   489  		return n, nil
   490  	}
   491  
   492  	deltaScale := newscale - original
   493  	absDeltaScale := int32(math.Abs(float64(deltaScale)))
   494  
   495  	multiplier := scaleMultipliers[absDeltaScale]
   496  	var wouldHaveLoss bool
   497  	out, wouldHaveLoss = n.rescaleWouldCauseDataLoss(deltaScale, multiplier)
   498  	if wouldHaveLoss {
   499  		err = errors.New("rescale data loss")
   500  	}
   501  	return
   502  }
   503  
   504  func (n Num) Abs() Num {
   505  	switch n.Sign() {
   506  	case -1:
   507  		return n.Negate()
   508  	}
   509  	return n
   510  }
   511  
   512  func (n Num) FitsInPrecision(prec int32) bool {
   513  	debug.Assert(prec > 0, "precision must be > 0")
   514  	debug.Assert(prec <= 76, "precision must be <= 76")
   515  	return n.Abs().Less(scaleMultipliers[prec])
   516  }
   517  
   518  func (n Num) ToString(scale int32) string {
   519  	f := (&big.Float{}).SetInt(n.BigInt())
   520  	if scale < 0 {
   521  		f.SetPrec(256).Mul(f, (&big.Float{}).SetInt(scaleMultipliers[-scale].BigInt()))
   522  	} else {
   523  		f.SetPrec(256).Quo(f, (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt()))
   524  	}
   525  	return f.Text('f', int(scale))
   526  }
   527  
   528  func GetScaleMultiplier(pow int) Num { return scaleMultipliers[pow] }
   529  
   530  func GetHalfScaleMultiplier(pow int) Num { return scaleMultipliersHalf[pow] }
   531  
   532  var (
   533  	scaleMultipliers = [...]Num{
   534  		FromU64(1),
   535  		FromU64(10),
   536  		FromU64(100),
   537  		FromU64(1000),
   538  		FromU64(10000),
   539  		FromU64(100000),
   540  		FromU64(1000000),
   541  		FromU64(10000000),
   542  		FromU64(100000000),
   543  		FromU64(1000000000),
   544  		FromU64(10000000000),
   545  		FromU64(100000000000),
   546  		FromU64(1000000000000),
   547  		FromU64(10000000000000),
   548  		FromU64(100000000000000),
   549  		FromU64(1000000000000000),
   550  		FromU64(10000000000000000),
   551  		FromU64(100000000000000000),
   552  		FromU64(1000000000000000000),
   553  		New(0, 0, 0, 10000000000000000000),
   554  		New(0, 0, 5, 7766279631452241920),
   555  		New(0, 0, 54, 3875820019684212736),
   556  		New(0, 0, 542, 1864712049423024128),
   557  		New(0, 0, 5421, 200376420520689664),
   558  		New(0, 0, 54210, 2003764205206896640),
   559  		New(0, 0, 542101, 1590897978359414784),
   560  		New(0, 0, 5421010, 15908979783594147840),
   561  		New(0, 0, 54210108, 11515845246265065472),
   562  		New(0, 0, 542101086, 4477988020393345024),
   563  		New(0, 0, 5421010862, 7886392056514347008),
   564  		New(0, 0, 54210108624, 5076944270305263616),
   565  		New(0, 0, 542101086242, 13875954555633532928),
   566  		New(0, 0, 5421010862427, 9632337040368467968),
   567  		New(0, 0, 54210108624275, 4089650035136921600),
   568  		New(0, 0, 542101086242752, 4003012203950112768),
   569  		New(0, 0, 5421010862427522, 3136633892082024448),
   570  		New(0, 0, 54210108624275221, 12919594847110692864),
   571  		New(0, 0, 542101086242752217, 68739955140067328),
   572  		New(0, 0, 5421010862427522170, 687399551400673280),
   573  		New(0, 2, 17316620476856118468, 6873995514006732800),
   574  		New(0, 29, 7145508105175220139, 13399722918938673152),
   575  		New(0, 293, 16114848830623546549, 4870020673419870208),
   576  		New(0, 2938, 13574535716559052564, 11806718586779598848),
   577  		New(0, 29387, 6618148649623664334, 7386721425538678784),
   578  		New(0, 293873, 10841254275107988496, 80237960548581376),
   579  		New(0, 2938735, 16178822382532126880, 802379605485813760),
   580  		New(0, 29387358, 14214271235644855872, 8023796054858137600),
   581  		New(0, 293873587, 13015503840481697412, 6450984253743169536),
   582  		New(0, 2938735877, 1027829888850112811, 9169610316303040512),
   583  		New(0, 29387358770, 10278298888501128114, 17909126868192198656),
   584  		New(0, 293873587705, 10549268516463523069, 13070572018536022016),
   585  		New(0, 2938735877055, 13258964796087472617, 1578511669393358848),
   586  		New(0, 29387358770557, 3462439444907864858, 15785116693933588480),
   587  		New(0, 293873587705571, 16177650375369096972, 10277214349659471872),
   588  		New(0, 2938735877055718, 14202551164014556797, 10538423128046960640),
   589  		New(0, 29387358770557187, 12898303124178706663, 13150510911921848320),
   590  		New(0, 293873587705571876, 18302566799529756941, 2377900603251621888),
   591  		New(0, 2938735877055718769, 17004971331911604867, 5332261958806667264),
   592  		New(1, 10940614696847636083, 4029016655730084128, 16429131440647569408),
   593  		New(15, 17172426599928602752, 3396678409881738056, 16717361816799281152),
   594  		New(159, 5703569335900062977, 15520040025107828953, 1152921504606846976),
   595  		New(1593, 1695461137871974930, 7626447661401876602, 11529215046068469760),
   596  		New(15930, 16954611378719749304, 2477500319180559562, 4611686018427387904),
   597  		New(159309, 3525417123811528497, 6328259118096044006, 9223372036854775808),
   598  		New(1593091, 16807427164405733357, 7942358959831785217, 0),
   599  		New(15930919, 2053574980671369030, 5636613303479645706, 0),
   600  		New(159309191, 2089005733004138687, 1025900813667802212, 0),
   601  		New(1593091911, 2443313256331835254, 10259008136678022120, 0),
   602  		New(15930919111, 5986388489608800929, 10356360998232463120, 0),
   603  		New(159309191113, 4523652674959354447, 11329889613776873120, 0),
   604  		New(1593091911132, 8343038602174441244, 2618431695511421504, 0),
   605  		New(15930919111324, 9643409726906205977, 7737572881404663424, 0),
   606  		New(159309191113245, 4200376900514301694, 3588752519208427776, 0),
   607  		New(1593091911132452, 5110280857723913709, 17440781118374726144, 0),
   608  		New(15930919111324522, 14209320429820033867, 8387114520361296896, 0),
   609  		New(159309191113245227, 12965995782233477362, 10084168908774762496, 0),
   610  		New(1593091911132452277, 532749306367912313, 8607968719199866880, 0),
   611  	}
   612  
   613  	scaleMultipliersHalf = [...]Num{
   614  		FromU64(0),
   615  		FromU64(5),
   616  		FromU64(50),
   617  		FromU64(500),
   618  		FromU64(5000),
   619  		FromU64(50000),
   620  		FromU64(500000),
   621  		FromU64(5000000),
   622  		FromU64(50000000),
   623  		FromU64(500000000),
   624  		FromU64(5000000000),
   625  		FromU64(50000000000),
   626  		FromU64(500000000000),
   627  		FromU64(5000000000000),
   628  		FromU64(50000000000000),
   629  		FromU64(500000000000000),
   630  		FromU64(5000000000000000),
   631  		FromU64(50000000000000000),
   632  		FromU64(500000000000000000),
   633  		FromU64(5000000000000000000),
   634  		New(0, 0, 2, 13106511852580896768),
   635  		New(0, 0, 27, 1937910009842106368),
   636  		New(0, 0, 271, 932356024711512064),
   637  		New(0, 0, 2710, 9323560247115120640),
   638  		New(0, 0, 27105, 1001882102603448320),
   639  		New(0, 0, 271050, 10018821026034483200),
   640  		New(0, 0, 2710505, 7954489891797073920),
   641  		New(0, 0, 27105054, 5757922623132532736),
   642  		New(0, 0, 271050543, 2238994010196672512),
   643  		New(0, 0, 2710505431, 3943196028257173504),
   644  		New(0, 0, 27105054312, 2538472135152631808),
   645  		New(0, 0, 271050543121, 6937977277816766464),
   646  		New(0, 0, 2710505431213, 14039540557039009792),
   647  		New(0, 0, 27105054312137, 11268197054423236608),
   648  		New(0, 0, 271050543121376, 2001506101975056384),
   649  		New(0, 0, 2710505431213761, 1568316946041012224),
   650  		New(0, 0, 27105054312137610, 15683169460410122240),
   651  		New(0, 0, 271050543121376108, 9257742014424809472),
   652  		New(0, 0, 2710505431213761085, 343699775700336640),
   653  		New(0, 1, 8658310238428059234, 3436997757003366400),
   654  		New(0, 14, 12796126089442385877, 15923233496324112384),
   655  		New(0, 146, 17280796452166549082, 11658382373564710912),
   656  		New(0, 1469, 6787267858279526282, 5903359293389799424),
   657  		New(0, 14693, 12532446361666607975, 3693360712769339392),
   658  		New(0, 146936, 14643999174408770056, 40118980274290688),
   659  		New(0, 1469367, 17312783228120839248, 401189802742906880),
   660  		New(0, 14693679, 7107135617822427936, 4011898027429068800),
   661  		New(0, 146936793, 15731123957095624514, 3225492126871584768),
   662  		New(0, 1469367938, 9737286981279832213, 13808177195006296064),
   663  		New(0, 14693679385, 5139149444250564057, 8954563434096099328),
   664  		New(0, 146936793852, 14498006295086537342, 15758658046122786816),
   665  		New(0, 1469367938527, 15852854434898512116, 10012627871551455232),
   666  		New(0, 14693679385278, 10954591759308708237, 7892558346966794240),
   667  		New(0, 146936793852785, 17312197224539324294, 5138607174829735936),
   668  		New(0, 1469367938527859, 7101275582007278398, 14492583600878256128),
   669  		New(0, 14693679385278593, 15672523598944129139, 15798627492815699968),
   670  		New(0, 146936793852785938, 9151283399764878470, 10412322338480586752),
   671  		New(0, 1469367938527859384, 17725857702810578241, 11889503016258109440),
   672  		New(0, 14693679385278593849, 11237880364719817872, 8214565720323784704),
   673  		New(7, 17809585336819077184, 1698339204940869028, 8358680908399640576),
   674  		New(79, 12075156704804807296, 16983392049408690284, 9799832789158199296),
   675  		New(796, 10071102605790763273, 3813223830700938301, 5764607523034234880),
   676  		New(7965, 8477305689359874652, 1238750159590279781, 2305843009213693952),
   677  		New(79654, 10986080598760540056, 12387501595902797811, 4611686018427387904),
   678  		New(796545, 17627085619057642486, 13194551516770668416, 9223372036854775808),
   679  		New(7965459, 10250159527190460323, 2818306651739822853, 0),
   680  		New(79654595, 10267874903356845151, 9736322443688676914, 0),
   681  		New(796545955, 10445028665020693435, 5129504068339011060, 0),
   682  		New(7965459555, 12216566281659176272, 14401552535971007368, 0),
   683  		New(79654595556, 11485198374334453031, 14888316843743212368, 0),
   684  		New(796545955566, 4171519301087220622, 1309215847755710752, 0),
   685  		New(7965459555662, 4821704863453102988, 13092158477557107520, 0),
   686  		New(79654595556622, 11323560487111926655, 1794376259604213888, 0),
   687  		New(796545955566226, 2555140428861956854, 17943762596042138880, 0),
   688  		New(7965459555662261, 7104660214910016933, 13416929297035424256, 0),
   689  		New(79654595556622613, 15706369927971514489, 5042084454387381248, 0),
   690  		New(796545955566226138, 9489746690038731964, 13527356396454709248, 0),
   691  	}
   692  
   693  	float64PowersOfTen = [...]float64{
   694  		1e-76, 1e-75, 1e-74, 1e-73, 1e-72, 1e-71, 1e-70, 1e-69, 1e-68, 1e-67, 1e-66, 1e-65,
   695  		1e-64, 1e-63, 1e-62, 1e-61, 1e-60, 1e-59, 1e-58, 1e-57, 1e-56, 1e-55, 1e-54, 1e-53,
   696  		1e-52, 1e-51, 1e-50, 1e-49, 1e-48, 1e-47, 1e-46, 1e-45, 1e-44, 1e-43, 1e-42, 1e-41,
   697  		1e-40, 1e-39, 1e-38, 1e-37, 1e-36, 1e-35, 1e-34, 1e-33, 1e-32, 1e-31, 1e-30, 1e-29,
   698  		1e-28, 1e-27, 1e-26, 1e-25, 1e-24, 1e-23, 1e-22, 1e-21, 1e-20, 1e-19, 1e-18, 1e-17,
   699  		1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5,
   700  		1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
   701  		1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
   702  		1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29, 1e30, 1e31,
   703  		1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39, 1e40, 1e41, 1e42, 1e43,
   704  		1e44, 1e45, 1e46, 1e47, 1e48, 1e49, 1e50, 1e51, 1e52, 1e53, 1e54, 1e55,
   705  		1e56, 1e57, 1e58, 1e59, 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67,
   706  		1e68, 1e69, 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76,
   707  	}
   708  )