Skip to content
Browse files

Fix sign behavior of % (TimToady)

  • Loading branch information...
1 parent 9c1607b commit 0053246b75938c267786c7a25fda831a3df9e145 @sorear committed Aug 2, 2011
Showing with 18 additions and 6 deletions.
  1. +18 −6 lib/Builtins.cs
View
24 lib/Builtins.cs
@@ -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);
}
@@ -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);
}
@@ -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);
}
}
@@ -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);
@@ -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);

0 comments on commit 0053246

Please sign in to comment.
Something went wrong with that request. Please try again.