fixed an assertion bug in binint division when divisor is greater than dividend

This commit is contained in:
hyunghwan.chung 2019-02-20 17:40:34 +00:00
parent 1b45243b34
commit fdda5c94f3
2 changed files with 77 additions and 23 deletions

View File

@ -209,27 +209,41 @@ extend MyObject
[ (16rFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF bitAnd: 16r1111111111111111111111111111111111111111) = 16r1111111111111111111111111111111111111111 ],
## 60-64
[(100213123891273912837891273189237 div: 1238971238971894573289472398477891263781263781263) = 0],
[(100213123891273912837891273189237 rem: 1238971238971894573289472398477891263781263781263) = 100213123891273912837891273189237],
[(-100213123891273912837891273189237 div: 1238971238971894573289472398477891263781263781263) = 0],
[(-100213123891273912837891273189237 rem: 1238971238971894573289472398477891263781263781263) = -100213123891273912837891273189237],
[(-100213123891273912837891273189237 mdiv: 1238971238971894573289472398477891263781263781263) = -1],
## 65-69
[(-100213123891273912837891273189237 mod: 1238971238971894573289472398477891263781263781263) = 1238971238971894473076348507203978425889990592026],
[(-123897123897189421321312312321312312132 div: -123897123897189421321312312321312312132) = 1],
[(-123897123897189421321312312321312312132 rem: -123897123897189421321312312321312312132) = 0],
[(-123897123897189421321312312321312312132 mdiv: -123897123897189421321312312321312312132) = 1],
[(-123897123897189421321312312321312312132 mod: -123897123897189421321312312321312312132) = 0],
## 70-74
[ (-0.1233 * 999999.123) = -123299.8918 ],
[ (-0.1233 * 999999.123) asString = '-123299.8918' ],
[ (-0.1233 - -0.123) = -0.0003 ],
[ (-0.1233 - -0.123) asString = '-0.0003' ],
[ (1.234 - 1.234) = 0 ], ## 0.000
## 65-69
## 75-79
[ (10.12 * 20.345) = 205.891 ],
[ (10.12 mlt: 20.345) = 205.89 ],
[ (-123897128378912738912738917.112323131233 div: 123.1) = -1006475453931053931053931.089458352000 ],
[ (-1006475453931053931053931.089458352000 * 123.1) = -123897128378912738912738917.112323131200 ],
[ 10 scale = 0 ],
## 70-74
## 80-84
[ 10.0 scale = 1 ],
[ 10.00 scale = 2 ],
[ (10 scale: 1) = 10.0 ],
[ (10 scale: 1) scale = (10.1 scale) ],
[ (10 scale: 2) scale = (10.11 scale) ],
## 75-79
## 85-89
[ ((-10.19 scale: 3) scale) = (10.199 scale) ],
[ ((-10.19 scale: 0) scale) = (10 scale) ],
[ (-9p10 scale) = (-10.000000000 scale) ],

View File

@ -619,6 +619,8 @@ static moo_oop_t normalize_bigint (moo_t* moo, moo_oop_t oop)
{
moo_oow_t count;
if (MOO_OOP_IS_SMOOI(oop)) return oop;
MOO_ASSERT (moo, MOO_OOP_IS_POINTER(oop));
count = count_effective_digits(oop);
@ -757,11 +759,24 @@ static MOO_INLINE int is_greater (moo_t* moo, moo_oop_t x, moo_oop_t y)
}
}
static MOO_INLINE int is_equal_unsigned_array (const moo_liw_t* x, moo_oow_t xs, const moo_liw_t* y, moo_oow_t ys)
{
return xs == ys && MOO_MEMCMP(x, y, xs * MOO_SIZEOF(*x)) == 0;
}
static MOO_INLINE int is_equal_unsigned (moo_oop_t x, moo_oop_t y)
{
return is_equal_unsigned_array(
MOO_OBJ_GET_LIWORD_SLOT(x), MOO_OBJ_GET_SIZE(x),
MOO_OBJ_GET_LIWORD_SLOT(y), MOO_OBJ_GET_SIZE(y));
}
static MOO_INLINE int is_equal (moo_t* moo, moo_oop_t x, moo_oop_t y)
{
/* check if two large integers are equal to each other */
return MOO_OBJ_GET_CLASS(x) == MOO_OBJ_GET_CLASS(y) && MOO_OBJ_GET_SIZE(x) == MOO_OBJ_GET_SIZE(y) &&
MOO_MEMCMP(MOO_OBJ_GET_LIWORD_SLOT(x), MOO_OBJ_GET_LIWORD_SLOT(y), MOO_OBJ_GET_SIZE(x) * MOO_SIZEOF(moo_liw_t)) == 0;
/*return MOO_OBJ_GET_CLASS(x) == MOO_OBJ_GET_CLASS(y) && MOO_OBJ_GET_SIZE(x) == MOO_OBJ_GET_SIZE(y) &&
MOO_MEMCMP(MOO_OBJ_GET_LIWORD_SLOT(x), MOO_OBJ_GET_LIWORD_SLOT(y), MOO_OBJ_GET_SIZE(x) * MOO_SIZEOF(moo_liw_t)) == 0;*/
return MOO_OBJ_GET_CLASS(x) == MOO_OBJ_GET_CLASS(y) && is_equal_unsigned(x, y);
}
static void complement2_unsigned_array (moo_t* moo, const moo_liw_t* x, moo_oow_t xs, moo_liw_t* z)
@ -1582,6 +1597,31 @@ static moo_oop_t divide_unsigned_integers (moo_t* moo, moo_oop_t x, moo_oop_t y,
{
moo_oop_t qq, rr;
if (is_less_unsigned(x, y))
{
rr = clone_bigint(moo, x, MOO_OBJ_GET_SIZE(x));
if (!rr) return MOO_NULL;
moo_pushvolat (moo, &rr);
qq = make_bigint_with_ooi(moo, 0); /* TODO: inefficient. no need to create a bigint object for zero. */
moo_popvolat (moo);
if (qq) *r = rr;
return qq;
}
else if (is_equal_unsigned(x, y))
{
rr = make_bigint_with_ooi(moo, 0); /* TODO: inefficient. no need to create a bigint object for zero. */
if (!rr) return MOO_NULL;
moo_pushvolat (moo, &rr);
qq = make_bigint_with_ooi(moo, 1); /* TODO: inefficient. no need to create a bigint object for zero. */
moo_popvolat (moo);
if (qq) *r = rr;
return qq;
}
/* the caller must ensure that x >= y */
MOO_ASSERT (moo, !is_less_unsigned(x, y));
moo_pushvolat (moo, &x);