Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix sign behavior of % (TimToady)
  • Loading branch information
sorear committed Aug 3, 2011
1 parent 9c1607b commit 0053246
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/Builtins.cs
Expand Up @@ -1044,7 +1044,8 @@ class SubstrLValue: Variable {

BigInteger rem;
BigInteger red = BigInteger.DivRem(c1, c2, out rem);
if (red.Sign < 0 && rem.Sign != 0) red--;
if (c2.Sign > 0 && rem.Sign < 0) red--;
if (c2.Sign < 0 && rem.Sign > 0) red++;

return MakeFatRat(c1 - red*cd, cd);
}
Expand All @@ -1058,7 +1059,8 @@ class SubstrLValue: Variable {

BigInteger rem;
BigInteger red = BigInteger.DivRem(c1, c2, out rem);
if (red.Sign < 0 && rem.Sign != 0) red--;
if (c2.Sign > 0 && rem.Sign < 0) red--;
if (c2.Sign < 0 && rem.Sign > 0) red++;

return MakeFixRat(c1 - red*c2, cd);
}
Expand All @@ -1067,15 +1069,17 @@ class SubstrLValue: Variable {
BigInteger v2 = PromoteToBigInt(r2, n2);
BigInteger rem;
BigInteger red = BigInteger.DivRem(v1, v2, out rem);
if (red.Sign < 0 && rem.Sign != 0) red--;
if (v2.Sign > 0 && rem.Sign < 0) red--;
if (v2.Sign < 0 && rem.Sign > 0) red++;
return MakeInt(v1 - v2*red);
}
{
long v1 = PromoteToFixInt(r1, n1);
long v2 = PromoteToFixInt(r2, n2);
long rem;
long red = Math.DivRem(v1, v2, out rem);
if (red < 0 && rem != 0) red--;
if (v2 > 0 && rem < 0) red--;
if (v2 < 0 && rem > 0) red++;
return MakeInt(v1 - v2*red);
}
}
Expand All @@ -1100,10 +1104,14 @@ class SubstrLValue: Variable {
if (!b2) big2 = small2;
BigInteger rem;
BigInteger red = BigInteger.DivRem(big1, big2, out rem);
if (opc >= 4 && red.Sign < 0 && rem.Sign != 0) {
if (opc >= 4 && big2.Sign > 0 && rem.Sign < 0) {
red--;
rem += big2;
}
if (opc >= 4 && big2.Sign < 0 && rem.Sign > 0) {
red++;
rem -= big2;
}
switch (opc & 3) {
case 0: return MakeInt(red);
case 1: return MakeInt(rem);
Expand All @@ -1112,10 +1120,14 @@ class SubstrLValue: Variable {
} else {
int rem = small1 % small2;
int red = small1 / small2;
if (opc >= 4 && red < 0 && rem != 0) {
if (opc >= 4 && small2 > 0 && rem < 0) {
red--;
rem += small2;
}
if (opc >= 4 && small2 < 0 && rem > 0) {
red++;
rem -= small2;
}
switch (opc & 3) {
case 0: return MakeInt(red);
case 1: return MakeInt(rem);
Expand Down

0 comments on commit 0053246

Please sign in to comment.