div/2 operators (from WG17 and #176)

This commit is contained in:
Vitor Santos Costa
2010-08-30 21:50:33 -05:00
parent 4e04e87ab6
commit e4a15cba33
4 changed files with 143 additions and 1 deletions

View File

@@ -220,7 +220,7 @@ Yap_gmp_div_int_big(Int i, Term t)
}
}
/* div i / b using temporary bigint new */
/* div b / i using temporary bigint new */
Term
Yap_gmp_div_big_int(Term t, Int i)
{
@@ -263,6 +263,37 @@ Yap_gmp_div_big_int(Term t, Int i)
}
}
/* div b / i using temporary bigint new */
Term
Yap_gmp_div2_big_int(Term t, Int i)
{
CELL *pt = RepAppl(t);
if (pt[1] == BIG_INT) {
MP_INT new;
MP_INT *b = Yap_BigIntOfTerm(t);
mpz_init_set(&new, b);
if (i > 0) {
mpz_fdiv_q_ui(&new, &new, i);
} else if (i == 0) {
return Yap_ArithError(EVALUATION_ERROR_ZERO_DIVISOR, MkIntTerm(0), "// /2");
} else {
/* we do not handle MIN_INT */
mpz_fdiv_q_ui(&new, &new, -i);
mpz_neg(&new, &new);
}
return MkBigAndClose(&new);
} else {
MP_RAT new;
MP_RAT *b = Yap_BigRatOfTerm(t);
mpq_init(&new);
mpq_set_si(&new, i, 1L);
mpq_div(&new, b, &new);
return MkRatAndClose(&new);
}
}
/* and i - b using temporary bigint new */
Term
Yap_gmp_and_int_big(Int i, Term t)
@@ -533,6 +564,44 @@ Yap_gmp_div_big_big(Term t1, Term t2)
}
}
/* div i div b using temporary bigint new */
Term
Yap_gmp_div2_big_big(Term t1, Term t2)
{
CELL *pt1 = RepAppl(t1);
CELL *pt2 = RepAppl(t2);
if (pt1[1] == BIG_INT && pt2[1] == BIG_INT) {
MP_INT new;
MP_INT *b1 = Yap_BigIntOfTerm(t1);
MP_INT *b2 = Yap_BigIntOfTerm(t2);
mpz_init_set(&new, b1);
mpz_fdiv_q(&new, &new, b2);
return MkBigAndClose(&new);
} else {
MP_RAT new;
MP_RAT *b1, bb1;
MP_RAT *b2, bb2;
if (pt1[1] == BIG_INT) {
b1 = &bb1;
mpq_init(b1);
mpq_set_z(b1, Yap_BigIntOfTerm(t1));
} else {
b1 = Yap_BigRatOfTerm(t1);
}
if (pt2[1] == BIG_INT) {
b2 = &bb2;
mpq_init(b2);
mpq_set_z(b2, Yap_BigIntOfTerm(t2));
} else {
b2 = Yap_BigRatOfTerm(t2);
}
mpq_init(&new);
mpq_div(&new, b1, b2);
return MkRatAndClose(&new);
}
}
Term
Yap_gmp_and_big_big(Term t1, Term t2)
{