From 6f745469b0d58e4af4bceeeb9097b59df476c64f Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 26 Feb 2010 00:15:55 +0000 Subject: [PATCH] make ceiling, floor and round always return an int (ISO and SWI compat, obs from Paulo Moura). --- C/arith1.c | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/C/arith1.c b/C/arith1.c index 980301c4f..52fbc0217 100644 --- a/C/arith1.c +++ b/C/arith1.c @@ -123,13 +123,13 @@ double my_rint(double x) if (x >= 0) { y = x + 0.5; z = floor(y); - n = (int) z; + n = (Int) z; if (y == z && n % 2) return(z-1); } else { y = x - 0.5; z = ceil(y); - n = (int) z; + n = (Int) z; if (y == z && n % 2) return(z+1); } @@ -463,11 +463,7 @@ eval1(Int fi, Term t) { case db_ref_e: RERROR(); } - if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ - RBIG_FL(floor(dbl)); - } else { - RFLOAT(floor(dbl)); - } + RBIG_FL(floor(dbl)); } case op_ceiling: { @@ -494,11 +490,7 @@ eval1(Int fi, Term t) { case db_ref_e: RERROR(); } - if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ - RBIG_FL(ceil(dbl)); - } else { - RFLOAT(ceil(dbl)); - } + RBIG_FL(ceil(dbl)); } case op_round: { @@ -507,9 +499,9 @@ eval1(Int fi, Term t) { switch (ETypeOfTerm(t)) { case long_int_e: 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 { - RFLOAT(IntegerOfTerm(t)); + return t; } case double_e: dbl = FloatOfTerm(t); @@ -518,21 +510,14 @@ eval1(Int fi, Term t) { #ifdef USE_GMP if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { return process_iso_error(Yap_BigIntOfTerm(t), t, "round"); - } else { - dbl = mpz_get_d(Yap_BigIntOfTerm(t)); } + return t; break; -#endif case db_ref_e: +#endif RERROR(); } - if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ - double vl = my_rint(dbl); - RBIG_FL(vl); - } else { - double vl = my_rint(dbl); - RFLOAT(vl); - } + RBIG_FL(my_rint(dbl)); } case op_truncate: case op_integer: @@ -540,19 +525,21 @@ eval1(Int fi, Term t) { Float dbl; switch (ETypeOfTerm(t)) { 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: dbl = FloatOfTerm(t); break; - case big_int_e: #ifdef USE_GMP - { - MP_INT new; - mpz_init_set(&new, Yap_BigIntOfTerm(t)); - RBIG(&new); + case big_int_e: + if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */ + return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is round(BIGNUM)"); } -#endif + return t; case db_ref_e: +#endif RERROR(); } #if HAVE_ISNAN