2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
2008-12-04 23:33:32 +00:00
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: arith1.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: arithmetical expression evaluation *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements unary arithmetic operations in YAP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "eval.h"
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
float_to_int(Float v)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
#if USE_GMP
|
2009-06-15 16:11:05 +01:00
|
|
|
Int i = (Int)v;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (i-v == 0.0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
return MkIntegerTerm(i);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_float_to_big(v);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#else
|
2008-12-04 23:33:32 +00:00
|
|
|
return MkIntegerTerm(v);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RBIG_FL(v) return(float_to_int(v))
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
typedef struct init_un_eval {
|
|
|
|
char *OpName;
|
2008-12-04 23:33:32 +00:00
|
|
|
arith1_op f;
|
2001-04-09 20:54:03 +01:00
|
|
|
} InitUnEntry;
|
|
|
|
|
|
|
|
/* Some compilers just don't get it */
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#undef HAVE_ASINH
|
|
|
|
#undef HAVE_ACOSH
|
|
|
|
#undef HAVE_ATANH
|
|
|
|
#undef HAVE_FINITE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !HAVE_ASINH
|
|
|
|
#define asinh(F) (log((F)+sqrt((F)*(F)+1)))
|
|
|
|
#endif
|
|
|
|
#if !HAVE_ACOSH
|
|
|
|
#define acosh(F) (log((F)+sqrt((F)*(F)-1)))
|
|
|
|
#endif
|
|
|
|
#if !HAVE_ATANH
|
|
|
|
#define atanh(F) (log((1+(F))/(1-(F)))/2)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static inline Float
|
|
|
|
get_float(Term t) {
|
|
|
|
if (IsFloatTerm(t)) {
|
|
|
|
return FloatOfTerm(t);
|
|
|
|
}
|
|
|
|
if (IsIntTerm(t)) {
|
|
|
|
return IntOfTerm(t);
|
|
|
|
}
|
|
|
|
if (IsLongIntTerm(t)) {
|
|
|
|
return LongIntOfTerm(t);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
2008-12-04 23:33:32 +00:00
|
|
|
if (IsBigIntTerm(t)) {
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_to_float(t);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WIN32 machines do not necessarily have rint. This will do for now */
|
|
|
|
#if HAVE_RINT
|
|
|
|
#define my_rint(X) rint(X)
|
|
|
|
#else
|
|
|
|
static
|
|
|
|
double my_rint(double x)
|
|
|
|
{
|
|
|
|
double y, z;
|
|
|
|
Int n;
|
|
|
|
|
|
|
|
if (x >= 0) {
|
|
|
|
y = x + 0.5;
|
|
|
|
z = floor(y);
|
2010-02-26 00:15:55 +00:00
|
|
|
n = (Int) z;
|
2008-12-04 23:33:32 +00:00
|
|
|
if (y == z && n % 2)
|
|
|
|
return(z-1);
|
|
|
|
} else {
|
|
|
|
y = x - 0.5;
|
|
|
|
z = ceil(y);
|
2010-02-26 00:15:55 +00:00
|
|
|
n = (Int) z;
|
2008-12-04 23:33:32 +00:00
|
|
|
if (y == z && n % 2)
|
|
|
|
return(z+1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
return(z);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Int
|
|
|
|
msb(Int inp) /* calculate the most significant bit for an integer */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
/* the obvious solution: do it by using binary search */
|
|
|
|
Int out = 0;
|
|
|
|
int off = sizeof(CELL)*4;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
if (inp < 0) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
|
2008-12-04 23:33:32 +00:00
|
|
|
"msb/1 received %d", inp);
|
|
|
|
}
|
2007-01-26 21:10:13 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
while (off) {
|
2010-05-11 00:18:12 +01:00
|
|
|
Int limit = ((CELL)1) << (off);
|
2008-12-04 23:33:32 +00:00
|
|
|
if (inp >= limit) {
|
|
|
|
out += off;
|
|
|
|
inp >>= off;
|
2007-01-26 21:10:13 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
off >>= 1;
|
|
|
|
}
|
|
|
|
return(out);
|
|
|
|
}
|
|
|
|
|
2009-10-20 09:50:51 +01:00
|
|
|
static Int
|
|
|
|
lsb(Int inp) /* calculate the least significant bit for an integer */
|
|
|
|
{
|
|
|
|
/* the obvious solution: do it by using binary search */
|
|
|
|
Int out = 0;
|
|
|
|
|
|
|
|
if (inp < 0) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
|
|
|
|
"msb/1 received %d", inp);
|
|
|
|
}
|
|
|
|
if (inp==0)
|
|
|
|
return 0L;
|
|
|
|
#if SIZEOF_LONG_INT == 8
|
|
|
|
if (!(inp & 0xffffffffLL)) {inp >>= 32; out += 32;}
|
|
|
|
#endif
|
|
|
|
if (!(inp & 0xffffL)) {inp >>= 16; out += 16;}
|
|
|
|
if (!(inp & 0xffL)) {inp >>= 8; out += 8;}
|
|
|
|
if (!(inp & 0xfL)) {inp >>= 4; out += 4;}
|
|
|
|
if (!(inp & 0x3L)) {inp >>= 2; out += 2;}
|
2010-05-11 00:18:12 +01:00
|
|
|
if (!(inp & ((CELL)0x1))) out++;
|
2009-10-20 09:50:51 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-10-20 10:03:10 +01:00
|
|
|
static Int
|
|
|
|
popcount(Int inp) /* calculate the least significant bit for an integer */
|
|
|
|
{
|
|
|
|
/* the obvious solution: do it by using binary search */
|
2010-05-11 00:18:12 +01:00
|
|
|
Int c = 0, j = 0, m = ((CELL)1);
|
2009-10-20 10:03:10 +01:00
|
|
|
|
|
|
|
if (inp < 0) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
|
|
|
|
"popcount/1 received %d", inp);
|
|
|
|
}
|
|
|
|
if (inp==0)
|
|
|
|
return 0L;
|
|
|
|
for(j=0,c=0; j<sizeof(inp)*8; j++, m<<=1)
|
|
|
|
{ if ( inp&m )
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
eval1(Int fi, Term t) {
|
|
|
|
arith1_op f = fi;
|
|
|
|
switch (f) {
|
|
|
|
case op_uplus:
|
|
|
|
return t;
|
|
|
|
case op_uminus:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
|
|
|
{
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
2008-12-04 23:33:32 +00:00
|
|
|
Int i = IntegerOfTerm(t);
|
|
|
|
|
|
|
|
if (i == Int_MIN) {
|
2010-05-27 12:24:15 +01:00
|
|
|
return Yap_gmp_neg_int(i);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
else
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(-IntegerOfTerm(t));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(-FloatOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2010-06-01 01:07:36 +01:00
|
|
|
#ifdef USE_GMP
|
2010-05-27 12:24:15 +01:00
|
|
|
return Yap_gmp_neg_big(t);
|
2010-06-01 01:07:36 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_unot:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(~IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2010-05-27 12:24:15 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "\\(%f)", FloatOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2010-05-27 12:24:15 +01:00
|
|
|
return Yap_gmp_unot_big(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_exp:
|
|
|
|
RFLOAT(exp(get_float(t)));
|
|
|
|
case op_log:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t);
|
|
|
|
if (dbl >= 0) {
|
|
|
|
RFLOAT(log(dbl));
|
|
|
|
} else {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "log(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case op_log10:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t);
|
|
|
|
if (dbl >= 0) {
|
|
|
|
RFLOAT(log10(dbl));
|
|
|
|
} else {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "log(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case op_sqrt:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = sqrt(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "acos(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_sin:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = sin(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_cos:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = cos(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_tan:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = tan(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_sinh:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = sinh(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_cosh:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = cosh(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_tanh:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
out = tanh(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_asin:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
|
|
|
out = asin(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_acos:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
|
|
|
out = acos(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_atan:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
|
|
|
out = atan(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_asinh:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
|
|
|
out = asinh(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_acosh:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
dbl = get_float(t);
|
|
|
|
out = acosh(dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_ISNAN
|
2008-12-04 23:33:32 +00:00
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
case op_atanh:
|
|
|
|
{
|
|
|
|
Float dbl, out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
|
|
|
out = atanh(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_lgamma:
|
|
|
|
{
|
|
|
|
Float dbl;
|
2005-01-05 17:32:03 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
dbl = get_float(t);
|
2005-01-05 17:32:03 +00:00
|
|
|
#if HAVE_LGAMMA
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(lgamma(dbl));
|
2005-01-07 06:29:20 +00:00
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2010-03-05 00:01:04 +00:00
|
|
|
case op_erf:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
#if HAVE_ERF
|
|
|
|
out = erf(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
#else
|
|
|
|
RERROR();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
case op_erfc:
|
|
|
|
{
|
|
|
|
Float dbl = get_float(t), out;
|
|
|
|
#if HAVE_ERF
|
|
|
|
out = erfc(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
#else
|
|
|
|
RERROR();
|
|
|
|
#endif
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
/*
|
|
|
|
floor(x) maximum integer greatest or equal to X
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
There are really two built-ins:
|
|
|
|
SICStus converts from int/big/float -> float
|
|
|
|
ISO only converts from float -> int/big
|
|
|
|
|
|
|
|
*/
|
|
|
|
case op_floor:
|
|
|
|
{
|
|
|
|
Float dbl;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
2010-05-28 09:53:56 +01:00
|
|
|
return t;
|
2008-12-04 23:33:32 +00:00
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_floor(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2010-02-26 09:12:20 +00:00
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(dbl)) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "integer(%f)", dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
#if HAVE_ISINF
|
2010-02-26 09:12:20 +00:00
|
|
|
if (isinf(dbl)) {
|
|
|
|
return Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl), "integer\
|
|
|
|
(%f)",dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-26 00:15:55 +00:00
|
|
|
RBIG_FL(floor(dbl));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_ceiling:
|
|
|
|
{
|
|
|
|
Float dbl;
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
2010-05-28 09:53:56 +01:00
|
|
|
return t;
|
2008-12-04 23:33:32 +00:00
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
case big_int_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_ceiling(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2010-02-26 09:12:20 +00:00
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(dbl)) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "integer(%f)", dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
#if HAVE_ISINF
|
2010-02-26 09:12:20 +00:00
|
|
|
if (isinf(dbl)) {
|
|
|
|
return Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl), "integer\
|
|
|
|
(%f)",dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-26 00:15:55 +00:00
|
|
|
RBIG_FL(ceil(dbl));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_round:
|
|
|
|
{
|
|
|
|
Float dbl;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
2010-05-28 09:53:56 +01:00
|
|
|
return t;
|
2008-12-04 23:33:32 +00:00
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_round(t);
|
2010-02-26 00:15:55 +00:00
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
default:
|
2008-12-04 23:33:32 +00:00
|
|
|
RERROR();
|
|
|
|
}
|
2010-02-26 09:12:20 +00:00
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(dbl)) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "integer(%f)", dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
#if HAVE_ISINF
|
2010-02-26 09:12:20 +00:00
|
|
|
if (isinf(dbl)) {
|
|
|
|
return Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl), "integer\
|
|
|
|
(%f)",dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-26 00:15:55 +00:00
|
|
|
RBIG_FL(my_rint(dbl));
|
2002-08-07 22:55:27 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_truncate:
|
|
|
|
case op_integer:
|
|
|
|
{
|
|
|
|
Float dbl;
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
2010-02-26 00:15:55 +00:00
|
|
|
return t;
|
2008-12-04 23:33:32 +00:00
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
2010-02-26 00:15:55 +00:00
|
|
|
case big_int_e:
|
2010-04-14 19:49:22 +01:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_trunc(t);
|
2010-02-26 00:15:55 +00:00
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
default:
|
2008-12-04 23:33:32 +00:00
|
|
|
RERROR();
|
|
|
|
}
|
2010-02-23 22:25:04 +00:00
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(dbl)) {
|
|
|
|
return Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t, "integer(%f)", dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-14 19:49:22 +01:00
|
|
|
#if HAVE_ISINF
|
2010-02-23 22:25:04 +00:00
|
|
|
if (isinf(dbl)) {
|
|
|
|
return Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl), "integer\
|
|
|
|
(%f)",dbl);
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-28 09:53:56 +01:00
|
|
|
if (dbl < 0.0)
|
|
|
|
RBIG_FL(ceil(dbl));
|
|
|
|
else
|
|
|
|
RBIG_FL(floor(dbl));
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
case op_float:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2010-03-09 22:03:00 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
RFLOAT(Yap_gmp_to_float(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2010-05-28 09:53:56 +01:00
|
|
|
case op_rational:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
|
|
|
return t;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case double_e:
|
|
|
|
return Yap_gmp_float_to_rational(FloatOfTerm(t));
|
|
|
|
#endif
|
|
|
|
case big_int_e:
|
|
|
|
return t;
|
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case op_rationalize:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
|
|
|
return t;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case double_e:
|
|
|
|
return Yap_gmp_float_rationalize(FloatOfTerm(t));
|
|
|
|
#endif
|
|
|
|
case big_int_e:
|
|
|
|
return t;
|
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_abs:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(labs(IntegerOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(fabs(FloatOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_abs_big(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_msb:
|
2009-02-09 21:56:40 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(msb(IntegerOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "msb(%f)", FloatOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_msb(t);
|
2009-10-20 09:50:51 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2009-10-20 09:50:51 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case op_lsb:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(lsb(IntegerOfTerm(t)));
|
|
|
|
case double_e:
|
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "lsb(%f)", FloatOfTerm(t));
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_lsb(t);
|
2009-10-20 10:03:10 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2009-10-20 10:03:10 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case op_popcount:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(popcount(IntegerOfTerm(t)));
|
|
|
|
case double_e:
|
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "popcount(%f)", FloatOfTerm(t));
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_popcount(t);
|
2008-12-04 23:33:32 +00:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2006-01-02 02:16:19 +00:00
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_ffracp:
|
2009-02-09 21:56:40 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is float_fractional_part(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
RFLOAT(0.0);
|
|
|
|
}
|
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
|
|
|
Float dbl;
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
RFLOAT(dbl-ceil(dbl));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_float_fractional_part(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2006-01-02 02:16:19 +00:00
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_fintp:
|
2009-02-09 21:56:40 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_FLOAT, t, "X is float_integer_part(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(rint(FloatOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_float_integer_part(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_sign:
|
2009-02-09 21:56:40 +00:00
|
|
|
switch (ETypeOfTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
|
|
|
Int x = IntegerOfTerm(t);
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((x > 0 ? 1 : (x < 0 ? -1 : 0)));
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case double_e:
|
|
|
|
{
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Float dbl = FloatOfTerm(t);
|
2008-08-12 02:27:23 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT((dbl > 0.0 ? 1 : (dbl < 0.0 ? -1 : 0)));
|
|
|
|
}
|
|
|
|
case big_int_e:
|
2008-08-12 02:27:23 +01:00
|
|
|
#ifdef USE_GMP
|
2010-05-28 09:53:56 +01:00
|
|
|
return Yap_gmp_sign(t);
|
2008-08-12 02:27:23 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2008-12-04 23:33:32 +00:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case op_random1:
|
|
|
|
switch (ETypeOfTerm(t)) {
|
2008-08-12 02:27:23 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RINT(Yap_random()*IntegerOfTerm(t));
|
2008-08-12 02:27:23 +01:00
|
|
|
case double_e:
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "random(%f)", FloatOfTerm(t));
|
2008-08-12 02:27:23 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "random(%f)", FloatOfTerm(t));
|
2008-08-12 02:27:23 +01:00
|
|
|
#endif
|
2010-03-06 22:43:21 +00:00
|
|
|
default:
|
2008-08-12 02:27:23 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
|
|
|
|
Term Yap_eval_unary(Int f, Term t)
|
|
|
|
{
|
2010-01-03 17:43:07 +00:00
|
|
|
return eval1(f,t);
|
2008-08-12 02:27:23 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static InitUnEntry InitUnTab[] = {
|
2008-12-04 23:33:32 +00:00
|
|
|
{"+", op_uplus},
|
|
|
|
{"-", op_uminus},
|
|
|
|
{"\\", op_unot},
|
|
|
|
{"exp", op_exp},
|
|
|
|
{"log", op_log},
|
|
|
|
{"log10", op_log10},
|
|
|
|
{"sqrt", op_sqrt},
|
|
|
|
{"sin", op_sin},
|
|
|
|
{"cos", op_cos},
|
|
|
|
{"tan", op_tan},
|
|
|
|
{"sinh", op_sinh},
|
|
|
|
{"cosh", op_cosh},
|
|
|
|
{"tanh", op_tanh},
|
|
|
|
{"asin", op_asin},
|
|
|
|
{"acos", op_acos},
|
|
|
|
{"atan", op_atan},
|
|
|
|
{"asinh", op_asinh},
|
|
|
|
{"acosh", op_acosh},
|
|
|
|
{"atanh", op_atanh},
|
|
|
|
{"floor", op_floor},
|
|
|
|
{"ceiling", op_ceiling},
|
|
|
|
{"round", op_round},
|
|
|
|
{"truncate", op_truncate},
|
|
|
|
{"integer", op_integer},
|
|
|
|
{"float", op_float},
|
|
|
|
{"abs", op_abs},
|
|
|
|
{"msb", op_msb},
|
2009-10-20 09:50:51 +01:00
|
|
|
{"lsb", op_lsb},
|
2009-10-20 10:03:10 +01:00
|
|
|
{"popcount", op_popcount},
|
2008-12-04 23:33:32 +00:00
|
|
|
{"float_fractional_part", op_ffracp},
|
|
|
|
{"float_integer_part", op_fintp},
|
|
|
|
{"sign", op_sign},
|
|
|
|
{"lgamma", op_lgamma},
|
2010-03-05 00:01:04 +00:00
|
|
|
{"erf",op_erf},
|
|
|
|
{"erfc",op_erfc},
|
2010-05-28 09:53:56 +01:00
|
|
|
{"rational",op_rational},
|
|
|
|
{"rationalize",op_rationalize},
|
2008-12-04 23:33:32 +00:00
|
|
|
{"random", op_random1}
|
2001-04-09 20:54:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_unary_is(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
Term t = Deref(ARG2);
|
2008-12-04 23:33:32 +00:00
|
|
|
Term top;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, ARG2, "X is Y");
|
2009-05-22 17:21:39 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
top = Yap_Eval(Deref(ARG3));
|
2010-02-22 22:48:13 +00:00
|
|
|
if (!Yap_FoundArithError(top, ARG3)) {
|
2008-12-04 23:33:32 +00:00
|
|
|
return FALSE;
|
2010-02-22 22:48:13 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsIntTerm(t)) {
|
2009-05-22 17:21:39 +01:00
|
|
|
Term tout = Yap_FoundArithError(eval1(IntegerOfTerm(t), top), Deref(ARG3));
|
2009-05-10 04:30:08 +01:00
|
|
|
if (!tout)
|
|
|
|
return FALSE;
|
|
|
|
return Yap_unify_constant(ARG1,tout);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom name = AtomOfTerm(t);
|
|
|
|
ExpEntry *p;
|
2009-05-10 04:30:08 +01:00
|
|
|
Term out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 1)))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term ti[2];
|
|
|
|
|
|
|
|
/* error */
|
|
|
|
ti[0] = t;
|
|
|
|
ti[1] = MkIntTerm(1);
|
2008-12-23 01:53:52 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
2008-12-04 23:33:32 +00:00
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,1);
|
2009-05-22 17:21:39 +01:00
|
|
|
P = FAILCODE;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2009-05-22 17:21:39 +01:00
|
|
|
if (!(out=Yap_FoundArithError(eval1(p->FOfEE, top),Deref(ARG3))))
|
2009-05-10 04:30:08 +01:00
|
|
|
return FALSE;
|
|
|
|
return Yap_unify_constant(ARG1,out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
2009-02-25 00:12:36 +00:00
|
|
|
static Int
|
|
|
|
p_unary_op_as_integer(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t, "X is Y");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsIntTerm(t)) {
|
|
|
|
return Yap_unify_constant(ARG2,t);
|
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom name = AtomOfTerm(t);
|
|
|
|
ExpEntry *p;
|
|
|
|
|
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 1)))) {
|
2010-03-06 00:16:49 +00:00
|
|
|
return Yap_unify(ARG1,ARG2);
|
2009-02-25 00:12:36 +00:00
|
|
|
}
|
|
|
|
return Yap_unify_constant(ARG2,MkIntTerm(p->FOfEE));
|
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitUnaryExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
ExpEntry *p;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(InitUnTab)/sizeof(InitUnEntry); ++i) {
|
2002-11-18 18:18:05 +00:00
|
|
|
AtomEntry *ae = RepAtom(Yap_LookupAtom(InitUnTab[i].OpName));
|
2005-03-01 22:25:09 +00:00
|
|
|
if (ae == NULL) {
|
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"at InitUnaryExps");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_LOCK(ae->ARWLock);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetExpPropHavingLock(ae, 1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (ExpEntry *) Yap_AllocAtomSpace(sizeof(ExpEntry));
|
2001-04-09 20:54:03 +01:00
|
|
|
p->KindOfPE = ExpProperty;
|
|
|
|
p->ArityOfEE = 1;
|
|
|
|
p->ENoOfEE = 1;
|
2008-12-04 23:33:32 +00:00
|
|
|
p->FOfEE = InitUnTab[i].f;
|
2001-10-30 16:42:05 +00:00
|
|
|
p->NextOfPE = ae->PropsOfAE;
|
|
|
|
ae->PropsOfAE = AbsExpProp(p);
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("is", 3, p_unary_is, TestPredFlag | SafePredFlag);
|
2009-02-25 00:12:36 +00:00
|
|
|
Yap_InitCPred("$unary_op_as_integer", 2, p_unary_op_as_integer, TestPredFlag|SafePredFlag);}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* This routine is called from Restore to make sure we have the same arithmetic operators */
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReInitUnaryExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2005-03-01 22:25:09 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|