|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math/big" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/shopspring/decimal" |
| 9 | +) |
| 10 | + |
| 11 | +// DecimalFractional constant for decimal calculations |
| 12 | +var DecimalFractional = decimal.NewFromBigInt(big.NewInt(1_000_000_000_000_000_000), 0) |
| 13 | + |
| 14 | +// SubSign returns mod subtraction and boolean indicating if the result is negative |
| 15 | +func SubSign(a, b decimal.Decimal) (decimal.Decimal, bool) { |
| 16 | + if a.GreaterThanOrEqual(b) { |
| 17 | + return a.Sub(b), false |
| 18 | + } |
| 19 | + return b.Sub(a), true |
| 20 | +} |
| 21 | + |
| 22 | +// Sqrt computes the square root of a decimal number using Newton's method. |
| 23 | +func Sqrt(value decimal.Decimal, precision decimal.Decimal) (decimal.Decimal, error) { |
| 24 | + if value.LessThan(decimal.Zero) { |
| 25 | + return decimal.Decimal{}, errors.New("square root of negative number") |
| 26 | + } |
| 27 | + |
| 28 | + x := value.DivRound(decimal.NewFromInt(2), precision.Exponent()) |
| 29 | + lastX := decimal.Zero |
| 30 | + |
| 31 | + for x.Sub(lastX).Abs().GreaterThan(precision) { |
| 32 | + lastX = x |
| 33 | + x = decimal.Avg(x, value.Div(x)) |
| 34 | + } |
| 35 | + |
| 36 | + return x, nil |
| 37 | +} |
| 38 | + |
| 39 | +// CalculatePow computes base^(exp) using an approximation algorithm |
| 40 | +func ApproximatePow(baseS, expS string, precisionS string) (decimal.Decimal, error) { |
| 41 | + // Convert string to Decimal |
| 42 | + base, err := decimal.NewFromString(baseS) |
| 43 | + if err != nil { |
| 44 | + return decimal.Decimal{}, err |
| 45 | + } |
| 46 | + |
| 47 | + exp, err := decimal.NewFromString(expS) |
| 48 | + if err != nil { |
| 49 | + return decimal.Decimal{}, err |
| 50 | + } |
| 51 | + |
| 52 | + precision, err := decimal.NewFromString(precisionS) |
| 53 | + if err != nil { |
| 54 | + return decimal.Decimal{}, err |
| 55 | + } |
| 56 | + |
| 57 | + if base.IsZero() && !exp.IsZero() { |
| 58 | + return base, nil |
| 59 | + } |
| 60 | + |
| 61 | + if base.GreaterThan(decimal.NewFromInt(2)) { |
| 62 | + return decimal.Decimal{}, errors.New("calculatePow: base must be less than 2") |
| 63 | + } |
| 64 | + |
| 65 | + integer := exp.Div(DecimalFractional).Floor() |
| 66 | + fractional := exp.Mod(DecimalFractional) |
| 67 | + integerPow := base.Pow(integer) |
| 68 | + |
| 69 | + if fractional.IsZero() { |
| 70 | + return integerPow, nil |
| 71 | + } |
| 72 | + |
| 73 | + fractionalPow, err := PowApprox(base, fractional, precision) |
| 74 | + if err != nil { |
| 75 | + return decimal.Decimal{}, err |
| 76 | + } |
| 77 | + |
| 78 | + result := integerPow.Mul(fractionalPow) |
| 79 | + return result, nil |
| 80 | +} |
| 81 | + |
| 82 | +// PowApprox approximates power for fractional exponents |
| 83 | +func PowApprox(base, exp, precision decimal.Decimal) (decimal.Decimal, error) { |
| 84 | + if exp.Equals(decimal.NewFromInt(1).Div(decimal.NewFromInt(2))) { |
| 85 | + sqrtBase, err := Sqrt(base, precision) |
| 86 | + if err != nil { |
| 87 | + return decimal.Decimal{}, err |
| 88 | + } |
| 89 | + return sqrtBase, nil |
| 90 | + } |
| 91 | + |
| 92 | + x, xNeg := SubSign(base, decimal.NewFromInt(1)) |
| 93 | + term := decimal.NewFromInt(1) |
| 94 | + sum := decimal.NewFromInt(1) |
| 95 | + negative := false |
| 96 | + |
| 97 | + a := exp |
| 98 | + bigK := decimal.Zero |
| 99 | + i := decimal.NewFromInt(1) |
| 100 | + |
| 101 | + for term.GreaterThanOrEqual(precision) { |
| 102 | + c, cNeg := SubSign(a, bigK) |
| 103 | + bigK = i |
| 104 | + |
| 105 | + newTerm := term.Mul(c).Mul(x).Div(bigK) |
| 106 | + term = newTerm |
| 107 | + |
| 108 | + if term.IsZero() { |
| 109 | + break |
| 110 | + } |
| 111 | + |
| 112 | + if xNeg { |
| 113 | + negative = !negative |
| 114 | + } |
| 115 | + |
| 116 | + if cNeg { |
| 117 | + negative = !negative |
| 118 | + } |
| 119 | + |
| 120 | + if negative { |
| 121 | + sum = sum.Sub(term) |
| 122 | + } else { |
| 123 | + sum = sum.Add(term) |
| 124 | + } |
| 125 | + |
| 126 | + i = i.Add(decimal.NewFromInt(1)) |
| 127 | + } |
| 128 | + fixedPoint := DecimalPlacesFromPrecision(precision) |
| 129 | + return sum.RoundDown(fixedPoint), nil |
| 130 | +} |
| 131 | + |
| 132 | +// DecimalPlacesFromPrecision calculates the number of decimal places based on the precision decimal. |
| 133 | +// DecimalPlacesFromPrecision calculates the number of decimal places based on the precision decimal. |
| 134 | +func DecimalPlacesFromPrecision(precision decimal.Decimal) int32 { |
| 135 | + // Convert precision to string using no fixed decimal places to avoid trailing zeros. |
| 136 | + str := precision.String() |
| 137 | + dotIndex := strings.IndexRune(str, '.') |
| 138 | + if dotIndex != -1 { |
| 139 | + // Remove trailing zeros to get the correct count of significant decimal places. |
| 140 | + str = strings.TrimRight(str, "0") |
| 141 | + // Count the number of characters after the decimal point to determine the scale. |
| 142 | + return int32(len(str) - dotIndex - 1) |
| 143 | + } |
| 144 | + return 0 // Return 0 if there is no fractional part. |
| 145 | +} |
0 commit comments