From d407fd253fc9af08a8534ae0806a55882b877a97 Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Tue, 23 Jul 2024 15:48:02 +0100 Subject: [PATCH 1/2] fix: increase default exponent precision to 18, as assets like ETH have 18 decimal places Signed-off-by: Elias Van Ootegem --- decimal.go | 34 +++---- decimal_test.go | 255 ++++++++++++++++++++++++------------------------ 2 files changed, 142 insertions(+), 147 deletions(-) diff --git a/decimal.go b/decimal.go index a37a230..1c3ddca 100644 --- a/decimal.go +++ b/decimal.go @@ -50,12 +50,12 @@ var DivisionPrecision = 16 // Example: // // d1, err := decimal.NewFromFloat(15.2).PowInt32(-2) -// d1.String() // output: "0.0043282548476454" +// d1.String() // output: "0.004328254847645429" // // decimal.PowPrecisionNegativeExponent = 24 // d2, err := decimal.NewFromFloat(15.2).PowInt32(-2) // d2.String() // output: "0.004328254847645429362881" -var PowPrecisionNegativeExponent = 16 +var PowPrecisionNegativeExponent = 18 // MarshalJSONWithoutQuotes should be set to true if you want the decimal to // be JSON marshaled as a number, instead of as a string. @@ -73,13 +73,15 @@ var ExpMaxIterations = 1000 // Zero should never be compared with == or != directly, please use decimal.Equal or decimal.Cmp instead. var Zero = New(0, 1) -var zeroInt = big.NewInt(0) -var oneInt = big.NewInt(1) -var twoInt = big.NewInt(2) -var fourInt = big.NewInt(4) -var fiveInt = big.NewInt(5) -var tenInt = big.NewInt(10) -var twentyInt = big.NewInt(20) +var ( + zeroInt = big.NewInt(0) + oneInt = big.NewInt(1) + twoInt = big.NewInt(2) + fourInt = big.NewInt(4) + fiveInt = big.NewInt(5) + tenInt = big.NewInt(10) + twentyInt = big.NewInt(20) +) var factorials = []Decimal{New(1, 0)} @@ -656,7 +658,7 @@ func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal { // now rv2 = abs(r.value) * 2 r2 := Decimal{value: &rv2, exp: r.exp + precision} // r2 is now 2 * r * 10 ^ precision - var c = r2.Cmp(d2.Abs()) + c := r2.Cmp(d2.Abs()) if c < 0 { return q @@ -1656,7 +1658,6 @@ func (d Decimal) RoundDown(places int32) Decimal { // NewFromFloat(5.55).RoundBank(1).String() // output: "5.6" // NewFromFloat(555).RoundBank(-1).String() // output: "560" func (d Decimal) RoundBank(places int32) Decimal { - round := d.Round(places) remainder := d.Sub(round).Abs() @@ -2233,7 +2234,6 @@ var _cos = [...]Decimal{ // Cos returns the cosine of the radian argument x. func (d Decimal) Cos() Decimal { - PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000, PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170, @@ -2283,17 +2283,17 @@ var _tanP = [...]Decimal{ NewFromFloat(1.15351664838587416140e+6), // 0x413199eca5fc9ddd NewFromFloat(-1.79565251976484877988e+7), // 0xc1711fead3299176 } + var _tanQ = [...]Decimal{ NewFromFloat(1.00000000000000000000e+0), - NewFromFloat(1.36812963470692954678e+4), //0x40cab8a5eeb36572 - NewFromFloat(-1.32089234440210967447e+6), //0xc13427bc582abc96 - NewFromFloat(2.50083801823357915839e+7), //0x4177d98fc2ead8ef - NewFromFloat(-5.38695755929454629881e+7), //0xc189afe03cbe5a31 + NewFromFloat(1.36812963470692954678e+4), // 0x40cab8a5eeb36572 + NewFromFloat(-1.32089234440210967447e+6), // 0xc13427bc582abc96 + NewFromFloat(2.50083801823357915839e+7), // 0x4177d98fc2ead8ef + NewFromFloat(-5.38695755929454629881e+7), // 0xc189afe03cbe5a31 } // Tan returns the tangent of the radian argument x. func (d Decimal) Tan() Decimal { - PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000, PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170, diff --git a/decimal_test.go b/decimal_test.go index d398f2d..d247f8f 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -411,33 +411,33 @@ func TestNewFromFloatWithExponent(t *testing.T) { } // some tests are taken from here https://www.cockroachlabs.com/blog/rounding-implementations-in-go/ tests := map[Inp]string{ - Inp{123.4, -3}: "123.4", - Inp{123.4, -1}: "123.4", - Inp{123.412345, 1}: "120", - Inp{123.412345, 0}: "123", - Inp{123.412345, -5}: "123.41235", - Inp{123.412345, -6}: "123.412345", - Inp{123.412345, -7}: "123.412345", - Inp{123.412345, -28}: "123.4123450000000019599610823207", - Inp{1230000000, 3}: "1230000000", - Inp{123.9999999999999999, -7}: "124", - Inp{123.8989898999999999, -7}: "123.8989899", - Inp{0.49999999999999994, 0}: "0", - Inp{0.5, 0}: "1", - Inp{0., -1000}: "0", - Inp{0.5000000000000001, 0}: "1", - Inp{1.390671161567e-309, 0}: "0", - Inp{4.503599627370497e+15, 0}: "4503599627370497", - Inp{4.503599627370497e+60, 0}: "4503599627370497110902645731364739935039854989106233267453952", - Inp{4.503599627370497e+60, 1}: "4503599627370497110902645731364739935039854989106233267453950", - Inp{4.503599627370497e+60, -1}: "4503599627370497110902645731364739935039854989106233267453952", - Inp{50, 2}: "100", - Inp{49, 2}: "0", - Inp{50, 3}: "0", + {123.4, -3}: "123.4", + {123.4, -1}: "123.4", + {123.412345, 1}: "120", + {123.412345, 0}: "123", + {123.412345, -5}: "123.41235", + {123.412345, -6}: "123.412345", + {123.412345, -7}: "123.412345", + {123.412345, -28}: "123.4123450000000019599610823207", + {1230000000, 3}: "1230000000", + {123.9999999999999999, -7}: "124", + {123.8989898999999999, -7}: "123.8989899", + {0.49999999999999994, 0}: "0", + {0.5, 0}: "1", + {0., -1000}: "0", + {0.5000000000000001, 0}: "1", + {1.390671161567e-309, 0}: "0", + {4.503599627370497e+15, 0}: "4503599627370497", + {4.503599627370497e+60, 0}: "4503599627370497110902645731364739935039854989106233267453952", + {4.503599627370497e+60, 1}: "4503599627370497110902645731364739935039854989106233267453950", + {4.503599627370497e+60, -1}: "4503599627370497110902645731364739935039854989106233267453952", + {50, 2}: "100", + {49, 2}: "0", + {50, 3}: "0", // subnormals - Inp{1.390671161567e-309, -2000}: "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390671161567000864431395448332752540137009987788957394095829635554502771758698872408926974382819387852542087331897381878220271350970912568035007740861074263206736245957501456549756342151614772544950978154339064833880234531754156635411349342950306987480369774780312897442981323940546749863054846093718407237782253156822124910364044261653195961209878120072488178603782495270845071470243842997312255994555557251870400944414666445871039673491570643357351279578519863428540219295076767898526278029257129758694673164251056158277568765100904638511604478844087596428177947970563689475826736810456067108202083804368114484417399279328807983736233036662284338182105684628835292230438999173947056675615385756827890872955322265625", - Inp{1.390671161567e-309, -862}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876", - Inp{1.390671161567e-309, -863}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876", + {1.390671161567e-309, -2000}: "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390671161567000864431395448332752540137009987788957394095829635554502771758698872408926974382819387852542087331897381878220271350970912568035007740861074263206736245957501456549756342151614772544950978154339064833880234531754156635411349342950306987480369774780312897442981323940546749863054846093718407237782253156822124910364044261653195961209878120072488178603782495270845071470243842997312255994555557251870400944414666445871039673491570643357351279578519863428540219295076767898526278029257129758694673164251056158277568765100904638511604478844087596428177947970563689475826736810456067108202083804368114484417399279328807983736233036662284338182105684628835292230438999173947056675615385756827890872955322265625", + {1.390671161567e-309, -862}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876", + {1.390671161567e-309, -863}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876", } // add negatives @@ -551,13 +551,13 @@ func TestNewFromBigIntWithExponent(t *testing.T) { exp int32 } tests := map[Inp]string{ - Inp{big.NewInt(123412345), -3}: "123412.345", - Inp{big.NewInt(2234), -1}: "223.4", - Inp{big.NewInt(323412345), 1}: "3234123450", - Inp{big.NewInt(423412345), 0}: "423412345", - Inp{big.NewInt(52341235), -5}: "523.41235", - Inp{big.NewInt(623412345), -6}: "623.412345", - Inp{big.NewInt(723412345), -7}: "72.3412345", + {big.NewInt(123412345), -3}: "123412.345", + {big.NewInt(2234), -1}: "223.4", + {big.NewInt(323412345), 1}: "3234123450", + {big.NewInt(423412345), 0}: "423412345", + {big.NewInt(52341235), -5}: "523.41235", + {big.NewInt(623412345), -6}: "623.412345", + {big.NewInt(723412345), -7}: "72.3412345", } // add negatives @@ -589,20 +589,20 @@ func TestNewFromBigRat(t *testing.T) { } tests := map[Inp]string{ - Inp{big.NewRat(0, 1), 16}: "0", - Inp{big.NewRat(4, 5), 16}: "0.8", - Inp{big.NewRat(10, 2), 16}: "5", - Inp{big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828", // rounded - Inp{big.NewRat(1, 434324545566634), 16}: "0.0000000000000023", - Inp{big.NewRat(1, 3), 16}: "0.3333333333333333", - Inp{big.NewRat(2, 3), 2}: "0.67", // rounded - Inp{big.NewRat(2, 3), 16}: "0.6666666666666667", // rounded - Inp{big.NewRat(10000, 3), 16}: "3333.3333333333333333", - Inp{mustParseRat("30702832066636633479"), 16}: "30702832066636633479", - Inp{mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753", - Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500", // rounded - Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753", // rounded - Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832", + {big.NewRat(0, 1), 16}: "0", + {big.NewRat(4, 5), 16}: "0.8", + {big.NewRat(10, 2), 16}: "5", + {big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828", // rounded + {big.NewRat(1, 434324545566634), 16}: "0.0000000000000023", + {big.NewRat(1, 3), 16}: "0.3333333333333333", + {big.NewRat(2, 3), 2}: "0.67", // rounded + {big.NewRat(2, 3), 16}: "0.6666666666666667", // rounded + {big.NewRat(10000, 3), 16}: "3333.3333333333333333", + {mustParseRat("30702832066636633479"), 16}: "30702832066636633479", + {mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753", + {mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500", // rounded + {mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753", // rounded + {mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832", } // add negatives @@ -634,7 +634,7 @@ func TestCopy(t *testing.T) { t.Error("expecting copy and origin to be equals, but they are not") } - //change value + // change value cpy = cpy.Add(New(1, 0)) if cpy.Cmp(origin) == 0 { @@ -949,10 +949,10 @@ func TestDecimal_rescale(t *testing.T) { rescale int32 } tests := map[Inp]string{ - Inp{1234, -3, -5}: "1.234", - Inp{1234, -3, 0}: "1", - Inp{1234, 3, 0}: "1234000", - Inp{1234, -4, -4}: "0.1234", + {1234, -3, -5}: "1.234", + {1234, -3, 0}: "1", + {1234, 3, 0}: "1234000", + {1234, -4, -4}: "0.1234", } // add negatives @@ -1677,12 +1677,12 @@ func TestDecimal_Add(t *testing.T) { } inputs := map[Inp]string{ - Inp{"2", "3"}: "5", - Inp{"2454495034", "3451204593"}: "5905699627", - Inp{"24544.95034", ".3451204593"}: "24545.2954604593", - Inp{".1", ".1"}: "0.2", - Inp{".1", "-.1"}: "0", - Inp{"0", "1.001"}: "1.001", + {"2", "3"}: "5", + {"2454495034", "3451204593"}: "5905699627", + {"24544.95034", ".3451204593"}: "24545.2954604593", + {".1", ".1"}: "0.2", + {".1", "-.1"}: "0", + {"0", "1.001"}: "1.001", } for inp, res := range inputs { @@ -1708,16 +1708,16 @@ func TestDecimal_Sub(t *testing.T) { } inputs := map[Inp]string{ - Inp{"2", "3"}: "-1", - Inp{"12", "3"}: "9", - Inp{"-2", "9"}: "-11", - Inp{"2454495034", "3451204593"}: "-996709559", - Inp{"24544.95034", ".3451204593"}: "24544.6052195407", - Inp{".1", "-.1"}: "0.2", - Inp{".1", ".1"}: "0", - Inp{"0", "1.001"}: "-1.001", - Inp{"1.001", "0"}: "1.001", - Inp{"2.3", ".3"}: "2", + {"2", "3"}: "-1", + {"12", "3"}: "9", + {"-2", "9"}: "-11", + {"2454495034", "3451204593"}: "-996709559", + {"24544.95034", ".3451204593"}: "24544.6052195407", + {".1", "-.1"}: "0.2", + {".1", ".1"}: "0", + {"0", "1.001"}: "-1.001", + {"1.001", "0"}: "1.001", + {"2.3", ".3"}: "2", } for inp, res := range inputs { @@ -1772,11 +1772,11 @@ func TestDecimal_Mul(t *testing.T) { } inputs := map[Inp]string{ - Inp{"2", "3"}: "6", - Inp{"2454495034", "3451204593"}: "8470964534836491162", - Inp{"24544.95034", ".3451204593"}: "8470.964534836491162", - Inp{".1", ".1"}: "0.01", - Inp{"0", "1.001"}: "0", + {"2", "3"}: "6", + {"2454495034", "3451204593"}: "8470964534836491162", + {"24544.95034", ".3451204593"}: "8470.964534836491162", + {".1", ".1"}: "0.01", + {"0", "1.001"}: "0", } for inp, res := range inputs { @@ -1808,13 +1808,13 @@ func TestDecimal_Shift(t *testing.T) { } inputs := map[Inp]string{ - Inp{"6", 3}: "6000", - Inp{"10", -2}: "0.1", - Inp{"2.2", 1}: "22", - Inp{"-2.2", -1}: "-0.22", - Inp{"12.88", 5}: "1288000", - Inp{"-10234274355545544493", -3}: "-10234274355545544.493", - Inp{"-4612301402398.4753343454", 5}: "-461230140239847533.43454", + {"6", 3}: "6000", + {"10", -2}: "0.1", + {"2.2", 1}: "22", + {"-2.2", -1}: "-0.22", + {"12.88", 5}: "1288000", + {"-10234274355545544493", -3}: "-10234274355545544.493", + {"-4612301402398.4753343454", 5}: "-461230140239847533.43454", } for inp, expectedStr := range inputs { @@ -1836,18 +1836,18 @@ func TestDecimal_Div(t *testing.T) { } inputs := map[Inp]string{ - Inp{"6", "3"}: "2", - Inp{"10", "2"}: "5", - Inp{"2.2", "1.1"}: "2", - Inp{"-2.2", "-1.1"}: "2", - Inp{"12.88", "5.6"}: "2.3", - Inp{"1023427554493", "43432632"}: "23563.5628642767953828", // rounded - Inp{"1", "434324545566634"}: "0.0000000000000023", - Inp{"1", "3"}: "0.3333333333333333", - Inp{"2", "3"}: "0.6666666666666667", // rounded - Inp{"10000", "3"}: "3333.3333333333333333", - Inp{"10234274355545544493", "-3"}: "-3411424785181848164.3333333333333333", - Inp{"-4612301402398.4753343454", "23.5"}: "-196268144782.9138440146978723", + {"6", "3"}: "2", + {"10", "2"}: "5", + {"2.2", "1.1"}: "2", + {"-2.2", "-1.1"}: "2", + {"12.88", "5.6"}: "2.3", + {"1023427554493", "43432632"}: "23563.5628642767953828", // rounded + {"1", "434324545566634"}: "0.0000000000000023", + {"1", "3"}: "0.3333333333333333", + {"2", "3"}: "0.6666666666666667", // rounded + {"10000", "3"}: "3333.3333333333333333", + {"10234274355545544493", "-3"}: "-3411424785181848164.3333333333333333", + {"-4612301402398.4753343454", "23.5"}: "-196268144782.9138440146978723", } for inp, expectedStr := range inputs { @@ -1880,11 +1880,11 @@ func TestDecimal_Div(t *testing.T) { // test code path where exp > 0 inputs2 := map[Inp2]string{ - Inp2{124, 10, 3, 1}: "41333333333.3333333333333333", - Inp2{124, 10, 3, 0}: "413333333333.3333333333333333", - Inp2{124, 10, 6, 1}: "20666666666.6666666666666667", - Inp2{124, 10, 6, 0}: "206666666666.6666666666666667", - Inp2{10, 10, 10, 1}: "1000000000", + {124, 10, 3, 1}: "41333333333.3333333333333333", + {124, 10, 3, 0}: "413333333333.3333333333333333", + {124, 10, 6, 1}: "20666666666.6666666666666667", + {124, 10, 6, 0}: "206666666666.6666666666666667", + {10, 10, 10, 1}: "1000000000", } for inp, expectedAbs := range inputs2 { @@ -2201,20 +2201,20 @@ func TestDecimal_Mod(t *testing.T) { } inputs := map[Inp]string{ - Inp{"3", "2"}: "1", - Inp{"3451204593", "2454495034"}: "996709559", - Inp{"9999999999", "1275"}: "324", - Inp{"9999999999.9999998", "1275.49"}: "239.2399998", - Inp{"24544.95034", "0.3451204593"}: "0.3283950433", - Inp{"0.499999999999999999", "0.25"}: "0.249999999999999999", - Inp{"0.989512958912895912", "0.000001"}: "0.000000958912895912", - Inp{"0.1", "0.1"}: "0", - Inp{"0", "1.001"}: "0", - Inp{"-7.5", "2"}: "-1.5", - Inp{"7.5", "-2"}: "1.5", - Inp{"-7.5", "-2"}: "-1.5", - Inp{"41", "21"}: "20", - Inp{"400000000001", "200000000001"}: "200000000000", + {"3", "2"}: "1", + {"3451204593", "2454495034"}: "996709559", + {"9999999999", "1275"}: "324", + {"9999999999.9999998", "1275.49"}: "239.2399998", + {"24544.95034", "0.3451204593"}: "0.3283950433", + {"0.499999999999999999", "0.25"}: "0.249999999999999999", + {"0.989512958912895912", "0.000001"}: "0.000000958912895912", + {"0.1", "0.1"}: "0", + {"0", "1.001"}: "0", + {"-7.5", "2"}: "-1.5", + {"7.5", "-2"}: "1.5", + {"-7.5", "-2"}: "-1.5", + {"41", "21"}: "20", + {"400000000001", "200000000001"}: "200000000000", } for inp, res := range inputs { @@ -2619,12 +2619,12 @@ func TestDecimal_Pow(t *testing.T) { {"67762386.283696923", "4.85917691669163916681738", "112761146905370140621385730157437443321.91755738117317148674362233906499698561022574811238435007575701773212242750262081945556470501"}, {"-3.0", "6.0", "729"}, {"-13.757", "5.0", "-492740.983929899460557"}, - {"3.0", "-6.0", "0.0013717421124829"}, - {"13.757", "-5.0", "0.000002029463821"}, - {"66.12", "-7.61313", "0.000000000000013854086588876805036"}, - {"6696871.12", "-2.61313", "0.000000000000000001455988684546983"}, - {"-3.0", "-6.0", "0.0013717421124829"}, - {"-13.757", "-5.0", "-0.000002029463821"}, + {"3.0", "-6.0", "0.001371742112482853"}, + {"13.757", "-5.0", "0.00000202946382098"}, + {"66.12", "-7.61313", "0.000000000000013853780421217382344176"}, + {"6696871.12", "-2.61313", "0.00000000000000000145579281162977937"}, + {"-3.0", "-6.0", "0.001371742112482853"}, + {"-13.757", "-5.0", "-0.00000202946382098"}, } { base, _ := NewFromString(testCase.Base) exp, _ := NewFromString(testCase.Exponent) @@ -2771,8 +2771,8 @@ func TestDecimal_PowInt32(t *testing.T) { {"629.25", 5, "98654323103449.5673828125"}, {"-3.0", 6, "729"}, {"-13.757", 5, "-492740.983929899460557"}, - {"3.0", -6, "0.0013717421124829"}, - {"-13.757", -5, "-0.000002029463821"}, + {"3.0", -6, "0.001371742112482853"}, + {"-13.757", -5, "-0.00000202946382098"}, } { base, _ := NewFromString(testCase.Decimal) expected, _ := NewFromString(testCase.Expected) @@ -2809,8 +2809,8 @@ func TestDecimal_PowBigInt(t *testing.T) { {"629.25", big.NewInt(5), "98654323103449.5673828125"}, {"-3.0", big.NewInt(6), "729"}, {"-13.757", big.NewInt(5), "-492740.983929899460557"}, - {"3.0", big.NewInt(-6), "0.0013717421124829"}, - {"-13.757", big.NewInt(-5), "-0.000002029463821"}, + {"3.0", big.NewInt(-6), "0.001371742112482853"}, + {"-13.757", big.NewInt(-5), "-0.00000202946382098"}, } { base, _ := NewFromString(testCase.Decimal) expected, _ := NewFromString(testCase.Expected) @@ -3101,7 +3101,6 @@ func TestDecimal_Sign(t *testing.T) { func didPanic(f func()) bool { ret := false func() { - defer func() { if message := recover(); message != nil { ret = true @@ -3110,11 +3109,9 @@ func didPanic(f func()) bool { // call the target function f() - }() return ret - } func TestDecimal_Coefficient(t *testing.T) { @@ -3189,7 +3186,6 @@ func TestNullDecimal_Scan(t *testing.T) { if err != nil { // Scan failed... no need to test result value t.Errorf("a.Scan(54.33) failed with message: %s", err) - } else { // Scan succeeded... test resulting values if !a.Valid { @@ -3208,7 +3204,6 @@ func TestNullDecimal_Scan(t *testing.T) { if err != nil { // Scan failed... no need to test result value t.Errorf("a.Scan(0) failed with message: %s", err) - } else { // Scan succeeded... test resulting values if !a.Valid { @@ -3231,7 +3226,6 @@ func TestNullDecimal_Scan(t *testing.T) { if err != nil { // Scan failed... no need to test result value t.Errorf("a.Scan('535.666') failed with message: %s", err) - } else { // Scan succeeded... test resulting values if !a.Valid { @@ -3406,7 +3400,7 @@ func TestGobEncode(t *testing.T) { func TestSum(t *testing.T) { vals := make([]Decimal, 10) - var i = int64(0) + i := int64(0) for key := range vals { vals[key] = New(i, 0) @@ -3421,7 +3415,7 @@ func TestSum(t *testing.T) { func TestAvg(t *testing.T) { vals := make([]Decimal, 10) - var i = int64(0) + i := int64(0) for key := range vals { vals[key] = New(i, 0) @@ -3510,7 +3504,8 @@ func TestSin(t *testing.T) { "10", "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424", } - sols := []string{"-0.22057186252002995641471297726318877448242875710373383657841216606788849153474483300147427943530288911869356126149550184271061369789963810497434594683859566879253561990821788142048867910104964466745284318577343435957806286762494529983369776697504436326725441516925396488258485248699247367113416543705253919473126183478178486954138205996912770183192357029798618739277146694040778731661407420114923656224752540889120768", + sols := []string{ + "-0.22057186252002995641471297726318877448242875710373383657841216606788849153474483300147427943530288911869356126149550184271061369789963810497434594683859566879253561990821788142048867910104964466745284318577343435957806286762494529983369776697504436326725441516925396488258485248699247367113416543705253919473126183478178486954138205996912770183192357029798618739277146694040778731661407420114923656224752540889120768", "-0.841470984807896544828551915928318375739843472469519282898610111931110319333748010828751784005573402229699531838022117989945539661588502120624574802425114599802714611508860519655182175315926637327774878594985045816542706701485174683683726979309922117859910272413672784175028365607893544855897795184024100973080880074046886009375162838756876336134083638363801171409953672944184918309063800980214873465660723218405962257950683415203634506166523593278", "-0.2474039592545229296662577977006816864013671875", "0", From 6eb5468c627fb6e7f140cb592c380a5f981610d8 Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Tue, 23 Jul 2024 15:58:20 +0100 Subject: [PATCH 2/2] fix: increase default precision to 18 decimals to match precision of assets like ETH Signed-off-by: Elias Van Ootegem --- decimal.go | 8 ++++---- decimal_test.go | 54 ++++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/decimal.go b/decimal.go index 1c3ddca..b307a17 100644 --- a/decimal.go +++ b/decimal.go @@ -33,15 +33,15 @@ import ( // Example: // // d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)) -// d1.String() // output: "0.6666666666666667" +// d1.String() // output: "0.666666666666666667" // d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000)) -// d2.String() // output: "0.0000666666666667" +// d2.String() // output: "0.000066666666666667" // d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3)) -// d3.String() // output: "6666.6666666666666667" +// d3.String() // output: "6666.666666666666666667" // decimal.DivisionPrecision = 3 // d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)) // d4.String() // output: "0.667" -var DivisionPrecision = 16 +var DivisionPrecision = 18 // PowPrecisionNegativeExponent specifies the maximum precision of the result (digits after decimal point) // when calculating decimal power. Only used for cases where the exponent is a negative number. diff --git a/decimal_test.go b/decimal_test.go index d247f8f..302ce37 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -1841,13 +1841,13 @@ func TestDecimal_Div(t *testing.T) { {"2.2", "1.1"}: "2", {"-2.2", "-1.1"}: "2", {"12.88", "5.6"}: "2.3", - {"1023427554493", "43432632"}: "23563.5628642767953828", // rounded - {"1", "434324545566634"}: "0.0000000000000023", - {"1", "3"}: "0.3333333333333333", - {"2", "3"}: "0.6666666666666667", // rounded - {"10000", "3"}: "3333.3333333333333333", - {"10234274355545544493", "-3"}: "-3411424785181848164.3333333333333333", - {"-4612301402398.4753343454", "23.5"}: "-196268144782.9138440146978723", + {"1023427554493", "43432632"}: "23563.56286427679538279", // rounded + {"1", "434324545566634"}: "0.000000000000002302", + {"1", "3"}: "0.333333333333333333", + {"2", "3"}: "0.666666666666666667", // rounded + {"10000", "3"}: "3333.333333333333333333", + {"10234274355545544493", "-3"}: "-3411424785181848164.333333333333333333", + {"-4612301402398.4753343454", "23.5"}: "-196268144782.91384401469787234", } for inp, expectedStr := range inputs { @@ -1880,10 +1880,10 @@ func TestDecimal_Div(t *testing.T) { // test code path where exp > 0 inputs2 := map[Inp2]string{ - {124, 10, 3, 1}: "41333333333.3333333333333333", - {124, 10, 3, 0}: "413333333333.3333333333333333", - {124, 10, 6, 1}: "20666666666.6666666666666667", - {124, 10, 6, 0}: "206666666666.6666666666666667", + {124, 10, 3, 1}: "41333333333.333333333333333333", + {124, 10, 3, 0}: "413333333333.333333333333333333", + {124, 10, 6, 1}: "20666666666.666666666666666667", + {124, 10, 6, 0}: "206666666666.666666666666666667", {10, 10, 10, 1}: "1000000000", } @@ -3465,15 +3465,15 @@ func TestAtan(t *testing.T) { "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424", } sols := []string{ - "-1.24076438822058001027437062753106", - "-0.78539816339744833061616997868383", - "-0.24497866312686415", + "-1.240764388220579999547768369366663257", + "-0.78539816339744828061616997868383", + "-0.24497866312686415525", "0.0", - "0.318747560420644443", - "0.78539816339744833061616997868383", - "1.37340076694501580123233995736766", - "1.47112767430373453123233995736766", - "1.57079623588597296123259450235374", + "0.31874756042064443706", + "0.78539816339744828061616997868383", + "1.37340076694501580283233995736766", + "1.47112767430373453363233995736766", + "1.570796235885972930232590411452263405", } for i, inp := range inps { d, err := NewFromString(inp) @@ -3585,15 +3585,15 @@ func TestTan(t *testing.T) { "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424", } sols := []string{ - "0.2261415650505790298980791606748881031998682652", - "-1.5574077246549025", - "-0.255341921221036275", + "0.226141565050579040350913677374961771568775128386", + "-1.557407724654902476", + "-0.2553419212210362665", "0.0", - "0.342524867530038963", - "1.5574077246549025", - "-3.3805150062465829", - "0.6483608274590872485524085572681343280321117494", - "0.68351325561491170753499935023939368502774607234006019034769919811202010905597996164029250820702097041244539696", + "0.34252486753003894782", + "1.557407724654902476", + "-3.380515006246583017", + "0.64836082745908726868517993033985905508029854441", + "0.6835132556149117039375485965437720350182539298544980382184903605930387213483425583662733693301829283609140225056", } for i, inp := range inps { d, err := NewFromString(inp)