@@ -61,14 +61,14 @@ contract LibDecimalFloatImplementationAddTest is Test {
6161 /// 1 + 1 = 2
6262 function testAddOneOneNotMaximized () external pure {
6363 (int256 signedCoefficient , int256 exponent ) = LibDecimalFloatImplementation.add (1 , 0 , 1 , 0 );
64- assertEq (signedCoefficient, 2e75 , "Signed coefficient mismatch " );
65- assertEq (exponent, - 75 , "Exponent mismatch " );
64+ assertEq (signedCoefficient, 2e76 , "Signed coefficient mismatch " );
65+ assertEq (exponent, - 76 , "Exponent mismatch " );
6666 }
6767
6868 function testAddOneOnePreMaximized () external pure {
69- (int256 signedCoefficient , int256 exponent ) = LibDecimalFloatImplementation.add (1e75 , - 75 , 1e75 , - 75 );
70- assertEq (signedCoefficient, 2e75 );
71- assertEq (exponent, - 75 );
69+ (int256 signedCoefficient , int256 exponent ) = LibDecimalFloatImplementation.add (1e76 , - 76 , 1e76 , - 76 );
70+ assertEq (signedCoefficient, 2e76 );
71+ assertEq (exponent, - 76 );
7272 }
7373
7474 /// 123456789 add 987654321
@@ -83,8 +83,8 @@ contract LibDecimalFloatImplementationAddTest is Test {
8383 /// 123456789e9 + 987654321 = 123456789987654321
8484 function testAdd123456789e9987654321 () external pure {
8585 (int256 signedCoefficient , int256 exponent ) = LibDecimalFloatImplementation.add (123456789 , 9 , 987654321 , 0 );
86- assertEq (signedCoefficient, 1.23456789987654321e75 );
87- assertEq (exponent, - 75 + 17 );
86+ assertEq (signedCoefficient, 1.23456789987654321e76 );
87+ assertEq (exponent, - 76 + 17 );
8888 }
8989
9090 function testGasAddZero () external pure {
@@ -139,63 +139,80 @@ contract LibDecimalFloatImplementationAddTest is Test {
139139
140140 function testAddingSmallToLargeReturnsLargeExamples () external pure {
141141 // Establish a baseline.
142- checkAdd (1e37 , 0 , 1e37 , - 37 , 10000000000000000000000000000000000001e38 , - 38 );
142+ checkAdd (1e37 , 0 , 1e37 , - 37 , 10000000000000000000000000000000000001e39 , - 39 );
143143 // Show baseline with reversed order.
144- checkAdd (1e37 , - 37 , 1e37 , 0 , 10000000000000000000000000000000000001e38 , - 38 );
144+ checkAdd (1e37 , - 37 , 1e37 , 0 , 10000000000000000000000000000000000001e39 , - 39 );
145145
146146 // Show full precision loss.
147- checkAdd (1e37 , 0 , 1e37 , - 38 , 100000000000000000000000000000000000001e37 , - 38 );
148- checkAdd (1e37 , 0 , 1e37 , - 75 , 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 38 );
149- checkAdd (1e37 , 0 , 1e37 , - 76 , 1e75 , - 38 );
147+ checkAdd (1e37 , 0 , 1e37 , - 38 , 100000000000000000000000000000000000001e38 , - 39 );
148+ checkAdd (1e37 , 0 , 1e37 , - 75 , 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 39 );
149+ checkAdd (1e38 , 0 , 1e37 , - 76 , 1e76 , - 38 );
150+ checkAdd (1e37 , 0 , 1e37 , - 76 , 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 39 );
150151 // Show same thing again with reversed order.
151- checkAdd (1e37 , - 38 , 1e37 , 0 , 100000000000000000000000000000000000001e37 , - 38 );
152- checkAdd (1e37 , - 75 , 1e37 , 0 , 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 38 );
153- checkAdd (1e37 , - 76 , 1e37 , 0 , 1e75 , - 38 );
152+ checkAdd (1e37 , - 38 , 1e37 , 0 , 100000000000000000000000000000000000001e38 , - 39 );
153+ checkAdd (1e37 , - 75 , 1e37 , 0 , 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 39 );
154+ checkAdd (1e37 , - 76 , 1e37 , 0 , 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 39 );
154155
155156 // Same precision loss happens for negative numbers.
156- checkAdd (- 1e37 , 0 , - 1e37 , - 38 , - 100000000000000000000000000000000000001e37 , - 38 );
157+ checkAdd (- 1e37 , 0 , - 1e37 , - 38 , - 100000000000000000000000000000000000001e38 , - 39 );
157158 checkAdd (
158- - 1e37 , 0 , - 1e37 , - 75 , - 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 38
159+ - 1e37 , 0 , - 1e37 , - 75 , - 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 39
160+ );
161+ checkAdd (
162+ - 1e37 , 0 , - 1e37 , - 76 , - 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 39
159163 );
160- checkAdd (- 1e37 , 0 , - 1e37 , - 76 , - 1e75 , - 38 );
161164 // Reverse order.
162- checkAdd (- 1e37 , - 38 , - 1e37 , 0 , - 100000000000000000000000000000000000001e37 , - 38 );
165+ checkAdd (- 1e37 , - 38 , - 1e37 , 0 , - 100000000000000000000000000000000000001e38 , - 39 );
166+ checkAdd (
167+ - 1e37 , - 75 , - 1e37 , 0 , - 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 39
168+ );
163169 checkAdd (
164- - 1e37 , - 75 , - 1e37 , 0 , - 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 38
170+ - 1e37 , - 76 , - 1e37 , 0 , - 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 39
165171 );
166- checkAdd (- 1e37 , - 76 , - 1e37 , 0 , - 1e75 , - 38 );
167172
168173 // Only the difference in exponents matters. Show the baseline.
169- checkAdd (1e37 , - 20 , 1e37 , - 57 , 10000000000000000000000000000000000001e38 , - 58 );
174+ checkAdd (1e37 , - 20 , 1e37 , - 57 , 10000000000000000000000000000000000001e39 , - 59 );
170175 checkAdd (
171- 1e37 , - 20 , 1e37 , - 95 , 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 58
176+ 1e37 , - 20 , 1e37 , - 95 , 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 59
172177 );
173- checkAdd (1e37 , - 20 , 1e37 , - 96 , 1e75 , - 58 );
178+ checkAdd (
179+ 1e37 , - 20 , 1e37 , - 96 , 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 59
180+ );
181+ checkAdd (1e37 , - 20 , 1e37 , - 97 , 1e76 , - 59 );
174182 // Reverse order.
175- checkAdd (1e37 , - 57 , 1e37 , - 20 , 10000000000000000000000000000000000001e38 , - 58 );
183+ checkAdd (1e37 , - 57 , 1e37 , - 20 , 10000000000000000000000000000000000001e39 , - 59 );
176184 checkAdd (
177- 1e37 , - 95 , 1e37 , - 20 , 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 58
185+ 1e37 , - 95 , 1e37 , - 20 , 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 59
178186 );
179- checkAdd (1e37 , - 96 , 1e37 , - 20 , 1e75 , - 58 );
187+ checkAdd (
188+ 1e37 , - 96 , 1e37 , - 20 , 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 59
189+ );
190+ checkAdd (1e37 , - 97 , 1e37 , - 20 , 1e76 , - 59 );
180191
181192 // Show the same thing with negative numbers.
182- checkAdd (- 1e37 , - 20 , - 1e37 , - 57 , - 10000000000000000000000000000000000001e38 , - 58 );
193+ checkAdd (- 1e37 , - 20 , - 1e37 , - 57 , - 10000000000000000000000000000000000001e39 , - 59 );
194+ checkAdd (
195+ - 1e37 , - 20 , - 1e37 , - 95 , - 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 59
196+ );
183197 checkAdd (
184- - 1e37 , - 20 , - 1e37 , - 95 , - 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 58
198+ - 1e37 , - 20 , - 1e37 , - 96 , - 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 59
185199 );
186- checkAdd (- 1e37 , - 20 , - 1e37 , - 96 , - 1e75 , - 58 );
200+ checkAdd (- 1e37 , - 20 , - 1e37 , - 97 , - 1e76 , - 59 );
187201
188202 // Reverse order.
189- checkAdd (- 1e37 , - 57 , - 1e37 , - 20 , - 10000000000000000000000000000000000001e38 , - 58 );
203+ checkAdd (- 1e37 , - 57 , - 1e37 , - 20 , - 10000000000000000000000000000000000001e39 , - 59 );
190204 checkAdd (
191- - 1e37 , - 95 , - 1e37 , - 20 , - 1000000000000000000000000000000000000000000000000000000000000000000000000001 , - 58
205+ - 1e37 , - 95 , - 1e37 , - 20 , - 10000000000000000000000000000000000000000000000000000000000000000000000000010 , - 59
192206 );
193- checkAdd (- 1e37 , - 96 , - 1e37 , - 20 , - 1e75 , - 58 );
207+ checkAdd (
208+ - 1e37 , - 96 , - 1e37 , - 20 , - 10000000000000000000000000000000000000000000000000000000000000000000000000001 , - 59
209+ );
210+ checkAdd (- 1e37 , - 97 , - 1e37 , - 20 , - 1e76 , - 59 );
194211 }
195212
196- /// If the exponents are the same and the coefficients are the same, then
197- /// addition is simply adding the coefficients.
198- function testAddSameExponentSameCoefficient (int256 signedCoefficientA , int256 signedCoefficientB ) external pure {
213+ /// If the exponents are the same then addition is simply adding the
214+ /// coefficients.
215+ function testAddSameExponent (int256 signedCoefficientA , int256 signedCoefficientB ) external pure {
199216 int256 exponentA;
200217 int256 exponentB;
201218 int256 signedCoefficientAMaximized;
@@ -208,7 +225,12 @@ contract LibDecimalFloatImplementationAddTest is Test {
208225 }
209226 exponentB = exponentA;
210227
211- int256 expectedSignedCoefficient = signedCoefficientAMaximized + signedCoefficientBMaximized;
228+ int256 expectedSignedCoefficient;
229+ unchecked {
230+ expectedSignedCoefficient = signedCoefficientAMaximized + signedCoefficientBMaximized;
231+ // We aren't testing the overflow case in this test.
232+ vm.assume (expectedSignedCoefficient - signedCoefficientBMaximized == signedCoefficientAMaximized);
233+ }
212234 int256 expectedExponent = exponentA;
213235
214236 (int256 signedCoefficient , int256 exponent ) = LibDecimalFloatImplementation.add (
0 commit comments