mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Compare commits
1 commit
f8aadd78b6
...
5b02b5b80e
Author | SHA1 | Date | |
---|---|---|---|
|
5b02b5b80e |
2 changed files with 1 additions and 13 deletions
13
decimal.go
13
decimal.go
|
@ -919,10 +919,7 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
|
||||||
// Halley's Iteration.
|
// Halley's Iteration.
|
||||||
// Calculating n-th term of formula: a_(n+1) = a_n - 2 * (exp(a_n) - z) / (exp(a_n) + z),
|
// 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
|
// until the difference between current and next term is smaller than epsilon
|
||||||
var prevStep Decimal
|
for {
|
||||||
maxIters := calcPrecision*2 + 10
|
|
||||||
|
|
||||||
for i := int32(0); i < maxIters; i++ {
|
|
||||||
// exp(a_n)
|
// exp(a_n)
|
||||||
comp3, _ = comp1.ExpTaylor(calcPrecision)
|
comp3, _ = comp1.ExpTaylor(calcPrecision)
|
||||||
// exp(a_n) - z
|
// exp(a_n) - z
|
||||||
|
@ -936,17 +933,9 @@ func (d Decimal) Ln(precision int32) (Decimal, error) {
|
||||||
// comp1 = a_(n+1) = a_n - 2 * (exp(a_n) - z) / (exp(a_n) + z)
|
// comp1 = a_(n+1) = a_n - 2 * (exp(a_n) - z) / (exp(a_n) + z)
|
||||||
comp1 = comp1.Sub(comp3)
|
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 {
|
if comp3.Abs().Cmp(epsilon) <= 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
prevStep = comp3
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2822,7 +2822,6 @@ func TestDecimal_Ln(t *testing.T) {
|
||||||
{"839101.0351094726488848490572028502", 50, "13.64008640145229044389152437468283605382056561604272"},
|
{"839101.0351094726488848490572028502", 50, "13.64008640145229044389152437468283605382056561604272"},
|
||||||
{"5023583755703750094849.03519358513093500275017501750602739169823", 25, "49.9684305274348922267409953"},
|
{"5023583755703750094849.03519358513093500275017501750602739169823", 25, "49.9684305274348922267409953"},
|
||||||
{"5023583755703750094849.03519358513093500275017501750602739169823", -1, "50.0"},
|
{"5023583755703750094849.03519358513093500275017501750602739169823", -1, "50.0"},
|
||||||
{"66.12", 18, "4.191471272952823429"},
|
|
||||||
} {
|
} {
|
||||||
d, _ := NewFromString(testCase.Dec)
|
d, _ := NewFromString(testCase.Dec)
|
||||||
expected, _ := NewFromString(testCase.Expected)
|
expected, _ := NewFromString(testCase.Expected)
|
||||||
|
|
Loading…
Reference in a new issue