Compare commits

...

5 commits

Author SHA1 Message Date
Sergey Vasiliev
0cf40c4b86
Merge 6e54f647f0 into 2b68c56fe0 2024-04-02 00:01:21 +02:00
Mateusz Woś
2b68c56fe0
Adjust Ln method to prevent infinity iteration loops (#357)
* Adjust Ln method to prevent infinity iteration loops
* Add test case for infinity loop
2024-04-01 22:02:25 +02:00
vasilbekk
6e54f647f0 Newline in end file 2022-10-01 14:20:16 +03:00
vasilbekk
d23c457004 Added unittests 2022-10-01 14:02:56 +03:00
vasilbekk
ba54fe1418 Added varible StringTrimTrailingZeros and comment for it 2022-10-01 13:47:11 +03:00
2 changed files with 71 additions and 7 deletions

View file

@ -51,6 +51,14 @@ var DivisionPrecision = 16
// silently lose precision.
var MarshalJSONWithoutQuotes = false
// StringTrimTrailingZeros should be set to false if you want the decimal stringify without zeros trailing.
// By default, when decimal is output as a string (for example, in JSON), zeros are truncated from it (2.00 -> 2, 3.11 -> 3.11, 13.000 -> 13).
// But this logic can be changed by this variable.
// For example, if you have numeric(10,2) values stored in your database,
// and you want your API response to always be given 2 decimal places (even 2.00, 3.00, 17.00 [not 2,3,17]),
// then this variable is a great way out.
var StringTrimTrailingZeros = true
// ExpMaxIterations specifies the maximum number of iterations needed to calculate
// precise natural exponent value using ExpHullAbrham method.
var ExpMaxIterations = 1000
@ -133,7 +141,6 @@ func NewFromBigInt(value *big.Int, exp int32) Decimal {
// d2 := NewFromBigRat(big.NewRat(4, 5), 1) // output: "0.8"
// d3 := NewFromBigRat(big.NewRat(1000, 3), 3) // output: "333.333"
// d4 := NewFromBigRat(big.NewRat(2, 7), 4) // output: "0.2857"
//
func NewFromBigRat(value *big.Rat, precision int32) Decimal {
return Decimal{
value: new(big.Int).Set(value.Num()),
@ -920,7 +927,10 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
// Halley's Iteration.
// Calculating n-th term of formula: a_(n+1) = a_n - 2 * (exp(a_n) - z) / (exp(a_n) + z),
// until the difference between current and next term is smaller than epsilon
for {
var prevStep Decimal
maxIters := calcPrecision*2 + 10
for i := int32(0); i < maxIters; i++ {
// exp(a_n)
comp3, _ = comp1.ExpTaylor(calcPrecision)
// exp(a_n) - z
@ -934,9 +944,17 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
// comp1 = a_(n+1) = a_n - 2 * (exp(a_n) - z) / (exp(a_n) + z)
comp1 = comp1.Sub(comp3)
if prevStep.Add(comp3).IsZero() {
// If iteration steps oscillate we should return early and prevent an infinity loop
// NOTE(mwoss): This should be quite a rare case, returning error is not necessary
break
}
if comp3.Abs().Cmp(epsilon) <= 0 {
break
}
prevStep = comp3
}
}
@ -1162,7 +1180,7 @@ func (d Decimal) InexactFloat64() float64 {
//
// -12.345
func (d Decimal) String() string {
return d.string(true)
return d.string(StringTrimTrailingZeros)
}
// StringFixed returns a rounded fixed-point string with places digits after

View file

@ -2822,6 +2822,7 @@ func TestDecimal_Ln(t *testing.T) {
{"839101.0351094726488848490572028502", 50, "13.64008640145229044389152437468283605382056561604272"},
{"5023583755703750094849.03519358513093500275017501750602739169823", 25, "49.9684305274348922267409953"},
{"5023583755703750094849.03519358513093500275017501750602739169823", -1, "50.0"},
{"66.12", 18, "4.191471272952823429"},
} {
d, _ := NewFromString(testCase.Dec)
expected, _ := NewFromString(testCase.Expected)
@ -3452,3 +3453,48 @@ func ExampleNewFromFloat() {
//0.123123123123123
//-10000000000000
}
func TestDecimal_String(t *testing.T) {
type testData struct {
input string
expected string
}
tests := []testData{
{"1.22", "1.22"},
{"1.00", "1"},
{"153.192", "153.192"},
{"999.999", "999.999"},
{"0.0000000001", "0.0000000001"},
{"0.0000000000", "0"},
}
for _, test := range tests {
d, err := NewFromString(test.input);
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
t.Errorf("expected %s, got %s", test.expected, d.String())
}
}
defer func() {
StringTrimTrailingZeros = true
}()
StringTrimTrailingZeros = false
tests = []testData{
{"1.00", "1.00"},
{"0.00", "0.00"},
{"129.123000", "129.123000"},
}
for _, test := range tests {
d, err := NewFromString(test.input);
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
t.Errorf("expected %s, got %s", test.expected, d.String())
}
}
}