1818
1919
2020#include " mathlib.h"
21+ #include " calculate.h"
2122#include " errortypes.h"
2223#include " token.h"
2324#include " utils.h"
@@ -186,13 +187,9 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat
186187 } else {
187188 switch (op) {
188189 case ' +' :
189- temp.mIntValue += v2.mIntValue ;
190- break ;
191190 case ' -' :
192- temp.mIntValue -= v2.mIntValue ;
193- break ;
194191 case ' *' :
195- temp.mIntValue *= v2.mIntValue ;
192+ temp.mIntValue = calculateArithmetic (op, temp. mIntValue , v2.mIntValue ) ;
196193 break ;
197194 case ' /' :
198195 if (v2.mIntValue == 0 )
@@ -204,7 +201,11 @@ MathLib::value MathLib::value::calc(char op, const MathLib::value &v1, const Mat
204201 case ' %' :
205202 if (v2.mIntValue == 0 )
206203 throw InternalError (nullptr , " Internal Error: Division by zero" );
207- temp.mIntValue %= v2.mIntValue ;
204+ // x % -1 is 0, but the division overflows for the minimum value
205+ if (v2.mIntValue == -1 )
206+ temp.mIntValue = 0 ;
207+ else
208+ temp.mIntValue %= v2.mIntValue ;
208209 break ;
209210 case ' &' :
210211 temp.mIntValue &= v2.mIntValue ;
@@ -1119,7 +1120,7 @@ std::string MathLib::add(const std::string & first, const std::string & second)
11191120 return (value (first) + value (second)).str ();
11201121#else
11211122 if (MathLib::isInt (first) && MathLib::isInt (second)) {
1122- return MathLib::toString (toBigNumber (first) + toBigNumber (second)) + intsuffix (first, second);
1123+ return MathLib::toString (calculateArithmetic ( ' + ' , toBigNumber (first), toBigNumber (second) )) + intsuffix (first, second);
11231124 }
11241125
11251126 double d1 = toDoubleNumber (first);
@@ -1141,7 +1142,7 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
11411142 return (value (first) - value (second)).str ();
11421143#else
11431144 if (MathLib::isInt (first) && MathLib::isInt (second)) {
1144- return MathLib::toString (toBigNumber (first) - toBigNumber (second)) + intsuffix (first, second);
1145+ return MathLib::toString (calculateArithmetic ( ' - ' , toBigNumber (first), toBigNumber (second) )) + intsuffix (first, second);
11451146 }
11461147
11471148 if (first == second)
@@ -1189,7 +1190,7 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
11891190 return (value (first) * value (second)).str ();
11901191#else
11911192 if (MathLib::isInt (first) && MathLib::isInt (second)) {
1192- return MathLib::toString (toBigNumber (first) * toBigNumber (second)) + intsuffix (first, second);
1193+ return MathLib::toString (calculateArithmetic ( ' * ' , toBigNumber (first), toBigNumber (second) )) + intsuffix (first, second);
11931194 }
11941195 return toString (toDoubleNumber (first) * toDoubleNumber (second));
11951196#endif
@@ -1204,6 +1205,9 @@ std::string MathLib::mod(const std::string &first, const std::string &second)
12041205 const bigint b = toBigNumber (second);
12051206 if (b == 0 )
12061207 throw InternalError (nullptr , " Internal Error: Division by zero" );
1208+ // x % -1 is 0, but the division overflows for the minimum value
1209+ if (b == -1 )
1210+ return " 0" + intsuffix (first, second);
12071211 return MathLib::toString (toBigNumber (first) % b) + intsuffix (first, second);
12081212 }
12091213 return toString (std::fmod (toDoubleNumber (first),toDoubleNumber (second)));
0 commit comments