make ceiling, floor and round always return an int (ISO and SWI compat, obs from Paulo Moura).

This commit is contained in:
Vitor Santos Costa 2010-02-26 00:15:55 +00:00
parent 7c4c852c82
commit 6f745469b0

View File

@ -123,13 +123,13 @@ double my_rint(double x)
if (x >= 0) { if (x >= 0) {
y = x + 0.5; y = x + 0.5;
z = floor(y); z = floor(y);
n = (int) z; n = (Int) z;
if (y == z && n % 2) if (y == z && n % 2)
return(z-1); return(z-1);
} else { } else {
y = x - 0.5; y = x - 0.5;
z = ceil(y); z = ceil(y);
n = (int) z; n = (Int) z;
if (y == z && n % 2) if (y == z && n % 2)
return(z+1); return(z+1);
} }
@ -463,11 +463,7 @@ eval1(Int fi, Term t) {
case db_ref_e: case db_ref_e:
RERROR(); RERROR();
} }
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ RBIG_FL(floor(dbl));
RBIG_FL(floor(dbl));
} else {
RFLOAT(floor(dbl));
}
} }
case op_ceiling: case op_ceiling:
{ {
@ -494,11 +490,7 @@ eval1(Int fi, Term t) {
case db_ref_e: case db_ref_e:
RERROR(); RERROR();
} }
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ RBIG_FL(ceil(dbl));
RBIG_FL(ceil(dbl));
} else {
RFLOAT(ceil(dbl));
}
} }
case op_round: case op_round:
{ {
@ -507,9 +499,9 @@ eval1(Int fi, Term t) {
switch (ETypeOfTerm(t)) { switch (ETypeOfTerm(t)) {
case long_int_e: case long_int_e:
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is round(%f)", IntegerOfTerm(t)); return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is round(%ld)", IntegerOfTerm(t));
} else { } else {
RFLOAT(IntegerOfTerm(t)); return t;
} }
case double_e: case double_e:
dbl = FloatOfTerm(t); dbl = FloatOfTerm(t);
@ -518,21 +510,14 @@ eval1(Int fi, Term t) {
#ifdef USE_GMP #ifdef USE_GMP
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { if (yap_flags[LANGUAGE_MODE_FLAG] == 1) {
return process_iso_error(Yap_BigIntOfTerm(t), t, "round"); return process_iso_error(Yap_BigIntOfTerm(t), t, "round");
} else {
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
} }
return t;
break; break;
#endif
case db_ref_e: case db_ref_e:
#endif
RERROR(); RERROR();
} }
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ RBIG_FL(my_rint(dbl));
double vl = my_rint(dbl);
RBIG_FL(vl);
} else {
double vl = my_rint(dbl);
RFLOAT(vl);
}
} }
case op_truncate: case op_truncate:
case op_integer: case op_integer:
@ -540,19 +525,21 @@ eval1(Int fi, Term t) {
Float dbl; Float dbl;
switch (ETypeOfTerm(t)) { switch (ETypeOfTerm(t)) {
case long_int_e: case long_int_e:
RINT(IntegerOfTerm(t)); if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is round(%ld)", IntegerOfTerm(t));
}
return t;
case double_e: case double_e:
dbl = FloatOfTerm(t); dbl = FloatOfTerm(t);
break; break;
case big_int_e:
#ifdef USE_GMP #ifdef USE_GMP
{ case big_int_e:
MP_INT new; if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
mpz_init_set(&new, Yap_BigIntOfTerm(t)); return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is round(BIGNUM)");
RBIG(&new);
} }
#endif return t;
case db_ref_e: case db_ref_e:
#endif
RERROR(); RERROR();
} }
#if HAVE_ISNAN #if HAVE_ISNAN