From 769e6f87e0523c0786d55b887e704f80a1d9bdef Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 9 Jan 2024 20:24:00 +0900 Subject: [PATCH] Speedup more stable swap math operations (backport #7214) (#7232) * Speedup more stable swap math operations (#7214) * Speedup more stable swap math operations * Try another speedup * revert one sigfig change * remove one more mutative operation * update osmomath * Update osmomath one more tiome * Update osmomath/decimal.go Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> --------- Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> (cherry picked from commit 9cd72313455113779e8f392eb4438668d412dbe5) # Conflicts: # go.mod # go.sum # osmomath/decimal.go # osmomath/decimal_test.go * fix conflicts (still need to run action) * fix go mods to run script * go mod updates v21 instead of main --------- Co-authored-by: Dev Ojha Co-authored-by: Adam Tucker Co-authored-by: Adam Tucker Co-authored-by: mattverse --- go.mod | 8 ++++---- go.sum | 20 ++++++++++---------- osmomath/binary_search.go | 6 ++++-- osmomath/decimal.go | 14 ++++++++++++++ osmomath/decimal_test.go | 20 ++++++++++++++++++++ osmomath/sigfig_round.go | 3 ++- osmoutils/go.mod | 2 +- osmoutils/go.sum | 4 ++-- x/epochs/go.mod | 2 +- x/epochs/go.sum | 6 +++--- x/gamm/pool-models/stableswap/amm.go | 9 +++++---- 11 files changed, 66 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index c77514bccc0..f91d3cc331c 100644 --- a/go.mod +++ b/go.mod @@ -26,10 +26,10 @@ require ( github.com/mattn/go-sqlite3 v1.14.17 github.com/ory/dockertest/v3 v3.10.0 github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 - github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa - github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa - github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa - github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d + github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d + github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d + github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d github.com/osmosis-labs/sqs/sqsdomain v0.0.0-20240108192026-6ccc0a29f77d github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 80d625db8fd..0271c5e895b 100644 --- a/go.sum +++ b/go.sum @@ -573,8 +573,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= +github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8= github.com/OpenPeeDeeP/depguard/v2 v2.1.0 h1:aQl70G173h/GZYhWf36aE5H0KaujXfVMnn/f1kSDVYY= github.com/OpenPeeDeeP/depguard/v2 v2.1.0/go.mod h1:PUBgk35fX4i7JDmwzlJwJ+GMe6NfO1723wmJMgPThNQ= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= @@ -659,8 +659,8 @@ github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUB github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9w277ABlgELO7H04IM= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= @@ -1457,14 +1457,14 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNs github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc= -github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa h1:jAvpqiiS8X6dcYgfl7zeIWIlRGKphkArxU7EQ16ZZuo= -github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:UX7hKiZbXIW4gDJ573KdVmvhxZLY7OUe6HTvR0YOnnE= -github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa h1:hS2GIQS8TYp1yHZ+Fi9L6oPuIlfdwlnd1/bhYq+e4kQ= -github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa/go.mod h1:fKo2dDDexDNScy44i2JDQU74a9WQ7ZABGwYQboc2EKc= -github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa h1:aFQQf8QOddtbVObH7QvGlsrTZnYGlXbDDlrZTfxuD00= -github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa/go.mod h1:YdU4Vli5YZijhyqYoKwg3LrjDmqkFk1UnIBREcSBb7o= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:coFEEv9ZQMacmfiKiz8tDCu/vAj/OCXdN5gyQKmMTQ8= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:h9tbR8pCWvseEwRv29qAsgzTvr/CYddq0PY24gg+xBY= +github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:ARQAHQAINF8iogS5Vc+1M6xD6lu10bcvQFvB/hVkoqk= +github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:QWydjLgWWlTQvizZc/W6bJzYn8vTVDk7X9zWyaBObSw= +github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d h1:V8Q+OvUAMBgSwqpiB6GD/Qy0Me8ws3oU5N+e6Vc2XK0= +github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d/go.mod h1:l7yluv3Sumqwpu+jZhk95SIFiwwFEdP7iVwVSwCwvHs= +github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d h1:HVBbpBHtC2o8GhjsrliAIPZ4jkyQtJTst+gW7sYMCH8= +github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d/go.mod h1:t80eA5n31RiYkj0AosDG2uPrnhC6jsGsJTnp9NK5wRM= github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d h1:J650EirkcVLQuc6B2fiVmM4kNQqntYioxd6v/D0RfLw= github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d/go.mod h1:mWCacHuCAOXS9TqOuGtSPVcO8rsfieHCaQjDE/wijZY= github.com/osmosis-labs/sqs/sqsdomain v0.0.0-20240108192026-6ccc0a29f77d h1:udZMOAqOYdBaYdkFMmylbuOGJBlCG70qehW9QR3cOdQ= diff --git a/osmomath/binary_search.go b/osmomath/binary_search.go index 17ef1d9c129..e6d449fb9b7 100644 --- a/osmomath/binary_search.go +++ b/osmomath/binary_search.go @@ -279,8 +279,10 @@ func BinarySearchBigDec(f func(BigDec) BigDec, curIteration := 0 for ; curIteration < maxIterations; curIteration += 1 { - // TODO: Try replacing this with RSH - curEstimate = lowerbound.Add(upperbound).QuoRaw(2) + // (lowerbound + upperbound) / 2 + curEstimate = lowerbound.Add(upperbound) + curEstimateBi := curEstimate.BigIntMut() + curEstimateBi.Rsh(curEstimateBi, 1) curOutput = f(curEstimate) // fmt.Println("binary search, input, target output, cur output", curEstimate, targetOutput, curOutput) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 714c1a23bf2..77fc0610911 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -253,6 +253,15 @@ func (d BigDec) BigInt() *big.Int { return cp.Set(d.i) } +// BigIntMut returns the pointer of the underlying big.Int. +func (d BigDec) BigIntMut() *big.Int { + if d.IsNil() { + return nil + } + + return d.i +} + // addition func (d BigDec) Add(d2 BigDec) BigDec { copy := d.Clone() @@ -281,6 +290,11 @@ func (d BigDec) Sub(d2 BigDec) BigDec { return BigDec{res} } +func (d BigDec) NegMut() BigDec { + d.i.Neg(d.i) + return d +} // reverse the decimal sign + // Clone performs a deep copy of the receiver // and returns the new result. func (d BigDec) Clone() BigDec { diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index dc61ab96b5e..eb5bb6bb8a6 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -1701,3 +1701,23 @@ func (s *decimalTestSuite) TestQuoTruncate_MutativeAndNonMutative() { }) } } + +func (s *decimalTestSuite) TestBigIntMut() { + r := big.NewInt(30) + d := osmomath.NewBigDecFromBigInt(r) + + // Compare value of BigInt & BigIntMut + s.Require().Equal(d.BigInt(), d.BigIntMut()) + + // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change + p1 := d.BigIntMut() + p1.SetInt64(40) + s.Require().Equal(big.NewInt(40), d.BigIntMut()) + s.Require().Equal(big.NewInt(40), d.BigInt()) + + // Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change + p2 := d.BigInt() + p2.SetInt64(50) + s.Require().NotEqual(big.NewInt(50), d.BigIntMut()) + s.Require().NotEqual(big.NewInt(50), d.BigInt()) +} diff --git a/osmomath/sigfig_round.go b/osmomath/sigfig_round.go index 692d5c72262..26ccbaa19fd 100644 --- a/osmomath/sigfig_round.go +++ b/osmomath/sigfig_round.go @@ -20,7 +20,8 @@ func SigFigRound(d Dec, tenToSigFig Int) Dec { dkSigFig := dTimesK.MulInt(tenToSigFig) numerator := dkSigFig.RoundInt().ToLegacyDec() + // TODO: Use pre-computed table for 10^k tenToK := NewInt(10).ToLegacyDec().Power(k) denominator := tenToSigFig.Mul(tenToK.TruncateInt()) - return numerator.QuoInt(denominator) + return numerator.QuoIntMut(denominator) } diff --git a/osmoutils/go.mod b/osmoutils/go.mod index 4528ea24448..f8b1da52521 100644 --- a/osmoutils/go.mod +++ b/osmoutils/go.mod @@ -11,7 +11,7 @@ require ( github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/iavl v0.20.1 github.com/cosmos/ibc-go/v7 v7.3.1 - github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha github.com/spf13/cast v1.6.0 github.com/spf13/cobra v1.8.0 diff --git a/osmoutils/go.sum b/osmoutils/go.sum index 8ee0aeaa7ce..378d5163def 100644 --- a/osmoutils/go.sum +++ b/osmoutils/go.sum @@ -1167,8 +1167,8 @@ github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1 h1:dAPp/n4+qqbJgPkMXcU+M0xUW github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs= github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNsB0Gb670ihBrnD5+qWtek= github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:coFEEv9ZQMacmfiKiz8tDCu/vAj/OCXdN5gyQKmMTQ8= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:h9tbR8pCWvseEwRv29qAsgzTvr/CYddq0PY24gg+xBY= github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha h1:3qX2j8bVY0D7Q08Fvy5r6tr2X2AutLifBRM+hOYvkjc= github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha/go.mod h1:lnfUSROf56UFfH1cPsRVx1W9BrwYkvsU6NwkzpjIYCU= github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231128160617-9a7cef4bff58 h1:DykYyH/LhDzWyAfX1H/EAJXfBr1JIrBmic1zGf36WEE= diff --git a/x/epochs/go.mod b/x/epochs/go.mod index 9bf98cea8d4..184692c15f0 100644 --- a/x/epochs/go.mod +++ b/x/epochs/go.mod @@ -8,7 +8,7 @@ require ( github.com/cosmos/gogoproto v1.4.11 github.com/golang/protobuf v1.5.3 github.com/grpc-ecosystem/grpc-gateway v1.16.0 - github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa + github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 diff --git a/x/epochs/go.sum b/x/epochs/go.sum index 55c2ae1e242..dc4cba89a87 100644 --- a/x/epochs/go.sum +++ b/x/epochs/go.sum @@ -1181,7 +1181,7 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= -github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= @@ -1202,8 +1202,8 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNs github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc= -github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa h1:jAvpqiiS8X6dcYgfl7zeIWIlRGKphkArxU7EQ16ZZuo= -github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:UX7hKiZbXIW4gDJ573KdVmvhxZLY7OUe6HTvR0YOnnE= +github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:ARQAHQAINF8iogS5Vc+1M6xD6lu10bcvQFvB/hVkoqk= +github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:QWydjLgWWlTQvizZc/W6bJzYn8vTVDk7X9zWyaBObSw= github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha h1:3qX2j8bVY0D7Q08Fvy5r6tr2X2AutLifBRM+hOYvkjc= github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha/go.mod h1:lnfUSROf56UFfH1cPsRVx1W9BrwYkvsU6NwkzpjIYCU= github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa h1:aFQQf8QOddtbVObH7QvGlsrTZnYGlXbDDlrZTfxuD00= diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index 80f009996b2..50337581abc 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -63,15 +63,16 @@ func targetKCalculator(x0, y0, w, yf osmomath.BigDec) osmomath.BigDec { // $$k_{iter}(x_f) = -x_{out}^3 + 3 x_0 x_{out}^2 - (y_f^2 + w + 3x_0^2)x_{out}$$ // where x_out = x_0 - x_f func iterKCalculator(x0, w, yf osmomath.BigDec) func(osmomath.BigDec) osmomath.BigDec { - // compute coefficients first - cubicCoeff := osmomath.OneBigDec().Neg() + // compute coefficients first. Notice that the leading coefficient is -1, we will use this to compute faster. + // cubicCoeff := -1 quadraticCoeff := x0.MulInt64(3) - linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).Neg() + linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).NegMut() return func(xf osmomath.BigDec) osmomath.BigDec { xOut := x0.Sub(xf) // horners method // ax^3 + bx^2 + cx = x(c + x(b + ax)) - res := cubicCoeff.Mul(xOut) + // a = -1 + res := xOut.Neg() res = res.AddMut(quadraticCoeff).MulMut(xOut) res = res.AddMut(linearCoeff).MulMut(xOut) return res