2001-04-09 20:54:03 +01: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 *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements unary arithmetic operations in YAP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "eval.h"
|
|
|
|
|
|
|
|
#define E_FUNC blob_type
|
|
|
|
#define E_ARGS , arith_retptr o
|
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
#define TMP_BIG() ((o)->big)
|
|
|
|
#define RINT(v) (o)->Int = v; return(long_int_e)
|
|
|
|
#define RFLOAT(v) (o)->dbl = v; return(double_e)
|
|
|
|
#define RBIG(v) return(big_int_e)
|
|
|
|
#define RERROR() return(db_ref_e)
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#if USE_GMP
|
|
|
|
static blob_type
|
|
|
|
float_to_int(Float v, union arith_ret *o)
|
|
|
|
{
|
|
|
|
Int i = (Int)v;
|
|
|
|
if (i-v == 0.0) {
|
|
|
|
o->Int = i;
|
2006-05-19 14:48:11 +01:00
|
|
|
return long_int_e;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set_d(o->big, v);
|
|
|
|
return big_int_e;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#define RBIG_FL(v) return(float_to_int(v,o))
|
|
|
|
#else
|
2006-01-08 03:23:07 +00:00
|
|
|
#define RBIG_FL(v) (o)->Int = (Int)(v); return long_int_e
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2006-01-04 11:18:45 +00:00
|
|
|
#if USE_GMP
|
2006-01-02 02:16:19 +00:00
|
|
|
static void
|
|
|
|
process_iso_error(MP_INT *big, Term t, char *operation)
|
|
|
|
{ /* iso */
|
|
|
|
Int sz = 2+mpz_sizeinbase(big,10);
|
|
|
|
char *s = Yap_AllocCodeSpace(sz);
|
|
|
|
|
|
|
|
if (s != NULL) {
|
|
|
|
mpz_get_str(s, 10, big);
|
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is %s(%s)", operation, s);
|
|
|
|
Yap_FreeCodeSpace(s);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is %s(t)",operation);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
}
|
|
|
|
}
|
2006-01-04 11:18:45 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
inline static Functor
|
|
|
|
AritFunctorOfTerm(Term t) {
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return(FunctorDBRef);
|
|
|
|
}
|
|
|
|
if (IsApplTerm(t)) {
|
|
|
|
return(FunctorOfTerm(t));
|
|
|
|
} else {
|
|
|
|
if (IsIntTerm(t))
|
|
|
|
return(FunctorLongInt);
|
|
|
|
else
|
|
|
|
return(FunctorDBRef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Term
|
|
|
|
EvalToTerm(blob_type f, union arith_ret *res)
|
|
|
|
{
|
|
|
|
switch (f) {
|
|
|
|
case long_int_e:
|
|
|
|
return(MkIntegerTerm(res->Int));
|
|
|
|
case double_e:
|
|
|
|
return(MkFloatTerm(res->dbl));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
Term t = Yap_MkBigIntTerm(res->big);
|
2006-01-18 15:34:54 +00:00
|
|
|
mpz_clear(res->big);
|
2006-01-02 02:16:19 +00:00
|
|
|
return t;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2006-01-02 02:16:19 +00:00
|
|
|
return TermNil;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef blob_type (*f_unexp)(Term, arith_retptr);
|
|
|
|
|
|
|
|
typedef struct init_un_eval {
|
|
|
|
char *OpName;
|
|
|
|
f_unexp f;
|
|
|
|
} 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
|
|
|
|
|
|
|
|
/*
|
|
|
|
do nothing...
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_uplus(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
blob_type bt;
|
|
|
|
union arith_ret v;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(IntegerOfTerm(t));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(FloatOfTerm(t));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
mpz_init_set(new, Yap_BigIntOfTerm(t));
|
|
|
|
RBIG(new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(v.Int);
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(v.dbl);
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
MPZ_SET(new, v.big);
|
|
|
|
RBIG(new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Error */
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
unary minus: -
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_uminus(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
2007-01-26 21:10:13 +00:00
|
|
|
{
|
2007-01-28 14:26:37 +00:00
|
|
|
#ifdef USE_GMP
|
2007-01-26 21:10:13 +00:00
|
|
|
Int i = IntegerOfTerm(t);
|
2007-01-28 14:26:37 +00:00
|
|
|
|
2007-01-26 21:10:13 +00:00
|
|
|
if (i == Int_MIN) {
|
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
|
|
|
|
mpz_init_set_si(new, i);
|
|
|
|
mpz_neg(new, new);
|
|
|
|
RBIG(new);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
RINT(-IntegerOfTerm(t));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
|
|
|
RFLOAT(-FloatOfTerm(t));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-05-13 11:37:27 +01:00
|
|
|
mpz_init_set(new, Yap_BigIntOfTerm(t));
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_neg(new, new);
|
2001-04-09 20:54:03 +01:00
|
|
|
RBIG(new);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(-v.Int);
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(-v.dbl);
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-05-13 11:37:27 +01:00
|
|
|
{
|
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
|
|
|
|
mpz_init_set(new, v.big);
|
|
|
|
mpz_neg(new, new);
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RBIG(new);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Error */
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
unary negation is \
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_unot(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(~IntegerOfTerm(t));
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "\\(f)", FloatOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-17 14:53:20 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set(new, Yap_BigIntOfTerm(t));
|
|
|
|
mpz_com(new, new);
|
2001-04-09 20:54:03 +01:00
|
|
|
RBIG(new);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(~v.Int);
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "\\(%f)", v.dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
|
|
|
|
MPZ_SET(new, v.big);
|
|
|
|
mpz_com(new, new);
|
|
|
|
RBIG(new);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
exponentiation exp(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_exp(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RFLOAT(exp(IntegerOfTerm(t)));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(exp(FloatOfTerm(t)));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
RFLOAT(exp(mpz_get_d(Yap_BigIntOfTerm(t))));
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RFLOAT(exp(v.Int));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(exp(v.dbl));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
double dbl = mpz_get_d(v.big);
|
|
|
|
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RFLOAT(exp(dbl));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
natural logarithm log(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_log(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
2008-05-13 11:37:27 +01:00
|
|
|
case big_int_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-18 21:34:02 +00:00
|
|
|
if (dbl >= 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
RFLOAT(log(dbl));
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "log(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
base 10 logarithm log10(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_log10(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-18 21:34:02 +00:00
|
|
|
if (dbl >= 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
RFLOAT(log10(dbl));
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "log10(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
square root sqrt(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_sqrt(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = sqrt(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t, "acos(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
sine sin(x) ? why did they take the e
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_sin(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(sin(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
cosine cos(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_cos(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(cos(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
tangent tan(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_tan(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(tan(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
hyperbolic sine sinh(x) = (exp(x) - exp(-x)) / 2.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_sinh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(sinh(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
hyperbolic cosine cosh(x) = (exp(x) + exp(-x)) / 2.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_cosh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(cosh(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
hyperbolic tangent tanh(x)
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_tanh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(tanh(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
asin(x) arc sine function
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_asin(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = asin(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2005-02-24 21:46:40 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t, "asin(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
acos(x) arc cosine function
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_acos(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = acos(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t, "acos(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
atan(x) arc tangent function
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_atan(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(atan(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
asinh(x) arc hyperbolic sine
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_asinh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(asinh(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
acosh(x) arc hyperbolic cosine
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_acosh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = acosh(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t, "acosh(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
atanh(x) arc hyperbolic tangent
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_atanh(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl, out;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = atanh(dbl);
|
|
|
|
#if HAVE_ISNAN
|
|
|
|
if (isnan(out)) {
|
2005-02-24 21:46:40 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t, "atanh(%f)", dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
|
|
|
|
2005-01-05 17:32:03 +00:00
|
|
|
/*
|
|
|
|
lgamma(x) is the logarithm of the gamma function.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_lgamma(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
2005-01-13 05:47:27 +00:00
|
|
|
Float dbl;
|
2005-01-05 17:32:03 +00:00
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = IntegerOfTerm(t);
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
|
|
|
bt = Yap_Eval(t, &v);
|
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
dbl = v.Int;
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
dbl = mpz_get_d(v.big);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2005-01-05 17:32:03 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Yap_Error */
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_LGAMMA
|
2005-01-13 05:47:27 +00:00
|
|
|
{
|
|
|
|
Float out;
|
|
|
|
out = lgamma(dbl);
|
|
|
|
RFLOAT(out);
|
|
|
|
}
|
2005-01-07 06:29:20 +00:00
|
|
|
#else
|
|
|
|
RERROR();
|
|
|
|
#endif
|
2005-01-05 17:32:03 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
floor(x) maximum integer greatest or equal to X
|
|
|
|
|
|
|
|
There are really two built-ins:
|
|
|
|
SICStus converts from int/big/float -> float
|
|
|
|
ISO only converts from float -> int/big
|
|
|
|
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_floor(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *big = Yap_BigIntOfTerm(t);
|
2001-04-09 20:54:03 +01:00
|
|
|
Int sz = 2+mpz_sizeinbase(big,10);
|
2002-11-18 18:18:05 +00:00
|
|
|
char *s = Yap_AllocCodeSpace(sz);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (s != NULL) {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_get_str(s, 10, big);
|
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(%s)", s);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeCodeSpace(s);
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(t)");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
Int sz = 2+mpz_sizeinbase(v.big,10);
|
2002-11-18 18:18:05 +00:00
|
|
|
char *s = Yap_AllocCodeSpace(sz);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (s != NULL) {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_get_str(s, 10, v.big);
|
|
|
|
mpz_clear(v.big);
|
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(%s)", s);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeCodeSpace(s);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is floor(t)");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(v.big);
|
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
|
|
|
RBIG_FL(floor(dbl));
|
|
|
|
} else {
|
|
|
|
RFLOAT(floor(dbl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ceiling(x) minimum integer smallest or equal to X
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_ceiling(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is ceiling(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "ceiling");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is ceiling(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(v.big, t, "ceiling");
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(v.big);
|
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
|
|
|
RBIG_FL(ceil(dbl));
|
|
|
|
} else {
|
2001-05-25 21:08:38 +01:00
|
|
|
RFLOAT(ceil(dbl));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-23 18:10:33 +01:00
|
|
|
/* 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);
|
|
|
|
n = (int) z;
|
|
|
|
if (y == z && n % 2)
|
|
|
|
return(z-1);
|
|
|
|
} else {
|
|
|
|
y = x - 0.5;
|
|
|
|
z = ceil(y);
|
|
|
|
n = (int) z;
|
|
|
|
if (y == z && n % 2)
|
|
|
|
return(z+1);
|
|
|
|
}
|
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
round(x) integer closest to 0
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_round(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is round(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) {
|
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "round");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is round(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(v.big, t, "round");
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(v.big);
|
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 17:13:56 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-10-23 18:10:33 +01:00
|
|
|
double vl = my_rint(dbl);
|
2002-06-25 17:13:56 +01:00
|
|
|
RBIG_FL(vl);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-10-23 18:10:33 +01:00
|
|
|
double vl = my_rint(dbl);
|
2002-06-25 17:13:56 +01:00
|
|
|
RFLOAT(vl);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
truncate(x) integer closest to 0
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_truncate(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is truncate(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "truncate");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(Yap_BigIntOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is truncate(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(v.big, t, "truncate");
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
dbl = mpz_get_d(v.big);
|
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-07 22:55:27 +01:00
|
|
|
if (dbl >= 0 ) {
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
|
|
|
RBIG_FL(floor(dbl));
|
|
|
|
} else {
|
|
|
|
RFLOAT(floor(dbl));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-08-07 22:55:27 +01:00
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
|
|
|
RBIG_FL(ceil(dbl));
|
|
|
|
} else {
|
|
|
|
RFLOAT(ceil(dbl));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
integer(x) SICStus integer closest to 0, similar to truncate
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_integer(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(IntegerOfTerm(t));
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
mpz_init_set(new, Yap_BigIntOfTerm(t));
|
|
|
|
RBIG(new);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(v.Int);
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
MP_INT *new = TMP_BIG();
|
|
|
|
|
|
|
|
MPZ_SET(new,v.big);
|
|
|
|
RBIG(new);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dbl <= (Float)Int_MAX && dbl >= (Float)Int_MIN) {
|
|
|
|
RINT((Int) dbl);
|
|
|
|
} else {
|
|
|
|
#ifdef USE_GMP
|
2006-01-17 14:53:20 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set_d(new, dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
RBIG(new);
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl), "integer/1");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
float(x) SICStus float
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_float(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(FloatOfTerm(t));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(mpz_get_d(Yap_BigIntOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(v.dbl);
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
Float dbl = mpz_get_d(v.big);
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RFLOAT(dbl);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
msb(Int inp) /* calculate the most significant bit for an integer */
|
|
|
|
{
|
|
|
|
/* the obvious solution: do it by using binary search */
|
|
|
|
Int out = 0;
|
|
|
|
int off = sizeof(CELL)*4;
|
|
|
|
|
|
|
|
if (inp < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
|
2001-04-09 20:54:03 +01:00
|
|
|
"msb/1 received %d", inp);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (off) {
|
2005-02-21 16:50:21 +00:00
|
|
|
Int limit = 1L << (off);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (inp >= limit) {
|
|
|
|
out += off;
|
|
|
|
inp >>= off;
|
|
|
|
}
|
|
|
|
off >>= 1;
|
|
|
|
}
|
|
|
|
return(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
abs(x): absolute value of a number
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_abs(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(labs(IntegerOfTerm(t)));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(fabs(FloatOfTerm(t)));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set(new, Yap_BigIntOfTerm(t));
|
|
|
|
mpz_abs(new, new);
|
2001-04-09 20:54:03 +01:00
|
|
|
RBIG(new);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(labs(v.Int));
|
|
|
|
case double_e:
|
|
|
|
RFLOAT(fabs(v.dbl));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *new = TMP_BIG();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
MPZ_SET(new, v.big);
|
|
|
|
mpz_abs(new, new);
|
2001-04-09 20:54:03 +01:00
|
|
|
RBIG(new);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
msb(x) most significant bit
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_msb(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(msb(IntegerOfTerm(t)));
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "msb(%f)", FloatOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_sizeinbase(Yap_BigIntOfTerm(t),2));
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(v.Int);
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "msb(%f)", v.dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
int sz = mpz_sizeinbase(v.big,2);
|
|
|
|
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RINT(sz);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
float_fractional_part(x) fraction for a float.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_ffracp(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is float_fractional_part(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(0.0);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "float_fractional_part");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
RFLOAT(0.0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is float_fractional_part(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(0.0);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(v.big, t, "float_fractional_part");
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(v.big);
|
2001-04-09 20:54:03 +01:00
|
|
|
RFLOAT(0.0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RFLOAT(dbl-ceil(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
float_integer_part(x) integer for a float.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_fintp(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is float_integer_part(%f)", IntegerOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(IntegerOfTerm(t));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "float_integer_part");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(mpz_get_d(Yap_BigIntOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_FLOAT, t, "X is float_integer_part(%f)", v.Int);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
} else {
|
|
|
|
RFLOAT(v.Int);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = v.dbl;
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
2006-01-02 02:16:19 +00:00
|
|
|
process_iso_error(Yap_BigIntOfTerm(t), t, "float_integer_part");
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-01-02 02:16:19 +00:00
|
|
|
Float dbl = mpz_get_d(v.big);
|
|
|
|
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RFLOAT(dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RFLOAT(rint(dbl));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
sign(x) sign of a number.
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_sign(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
Float dbl;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int x = IntegerOfTerm(t);
|
|
|
|
|
|
|
|
RINT((x > 0 ? 1 : (x < 0 ? -1 : 0)));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
dbl = FloatOfTerm(t);
|
|
|
|
|
|
|
|
RINT((dbl > 0.0 ? 1 : (dbl < 0.0 ? -1 : 0)));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_sgn(Yap_BigIntOfTerm(t)));
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
2002-11-18 18:18:05 +00:00
|
|
|
bt = Yap_Eval(t, &v);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT((v.Int > 0 ? 1 : (v.Int < 0 ? -1 : 0)));
|
|
|
|
case double_e:
|
|
|
|
RINT((v.dbl > 0.0 ? 1 : (v.dbl < 0.0 ? -1 : 0)));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
|
|
|
int sgn = mpz_sgn(v.big);
|
|
|
|
|
|
|
|
mpz_clear(v.big);
|
|
|
|
RINT(sgn);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_Error */
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 02:27:23 +01:00
|
|
|
/*
|
|
|
|
unary negation is \
|
|
|
|
*/
|
|
|
|
static E_FUNC
|
|
|
|
p_random(Term t E_ARGS)
|
|
|
|
{
|
|
|
|
Functor f = AritFunctorOfTerm(t);
|
|
|
|
union arith_ret v;
|
|
|
|
blob_type bt;
|
|
|
|
|
|
|
|
switch (BlobOfFunctor(f)) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(Yap_random()*IntegerOfTerm(t));
|
|
|
|
case double_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "random(%f)", FloatOfTerm(t));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "random(%f)", FloatOfTerm(t));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* we've got a full term, need to evaluate it first */
|
|
|
|
bt = Yap_Eval(t, &v);
|
|
|
|
/* second case, no need no evaluation */
|
|
|
|
switch (bt) {
|
|
|
|
case long_int_e:
|
|
|
|
RINT(Yap_random()*v.Int);
|
|
|
|
case double_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "random(%f)", v.dbl);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "random(%f)", FloatOfTerm(t));
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Yap_Error */
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static InitUnEntry InitUnTab[] = {
|
|
|
|
{"+", p_uplus},
|
|
|
|
{"-", p_uminus},
|
|
|
|
{"\\", p_unot},
|
|
|
|
{"exp", p_exp},
|
|
|
|
{"log", p_log},
|
|
|
|
{"log10", p_log10},
|
|
|
|
{"sqrt", p_sqrt},
|
|
|
|
{"sin", p_sin},
|
|
|
|
{"cos", p_cos},
|
|
|
|
{"tan", p_tan},
|
|
|
|
{"sinh", p_sinh},
|
|
|
|
{"cosh", p_cosh},
|
|
|
|
{"tanh", p_tanh},
|
|
|
|
{"asin", p_asin},
|
|
|
|
{"acos", p_acos},
|
|
|
|
{"atan", p_atan},
|
|
|
|
{"asinh", p_asinh},
|
|
|
|
{"acosh", p_acosh},
|
|
|
|
{"atanh", p_atanh},
|
|
|
|
{"floor", p_floor},
|
|
|
|
{"ceiling", p_ceiling},
|
|
|
|
{"round", p_round},
|
|
|
|
{"truncate", p_truncate},
|
|
|
|
{"integer", p_integer},
|
|
|
|
{"float", p_float},
|
|
|
|
{"abs", p_abs},
|
|
|
|
{"msb", p_msb},
|
|
|
|
{"float_fractional_part", p_ffracp},
|
|
|
|
{"float_integer_part", p_fintp},
|
2005-01-13 05:47:27 +00:00
|
|
|
{"sign", p_sign},
|
|
|
|
{"lgamma", p_lgamma},
|
2008-08-12 02:27:23 +01:00
|
|
|
{"random", p_random},
|
2001-04-09 20:54:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_unary_is(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
Term t = Deref(ARG2);
|
|
|
|
union arith_ret res;
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, ARG2, "X is Y");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsIntTerm(t)) {
|
|
|
|
blob_type f = InitUnTab[IntOfTerm(t)].f(Deref(ARG3),&res);
|
2002-11-18 18:18:05 +00:00
|
|
|
return (Yap_unify_constant(ARG1,EvalToTerm(f,&res)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom name = AtomOfTerm(t);
|
|
|
|
ExpEntry *p;
|
|
|
|
blob_type f;
|
|
|
|
|
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);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("/"),2), 2, ti);
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
2001-04-09 20:54:03 +01:00
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,1);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
f = p->FOfEE.unary(Deref(ARG3),&res);
|
2002-11-18 18:18:05 +00:00
|
|
|
return (Yap_unify_constant(ARG1,EvalToTerm(f,&res)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
p->FOfEE.unary = 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);
|
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
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
Prop p;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(InitUnTab)/sizeof(InitUnEntry); ++i) {
|
2002-11-18 18:18:05 +00:00
|
|
|
AtomEntry *ae = RepAtom(Yap_FullLookupAtom(InitUnTab[i].OpName));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-03-01 22:25:09 +00:00
|
|
|
if (ae == NULL) {
|
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"at ReInitUnaryExps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_LOCK(ae->ARWLock);
|
2002-11-18 18:18:05 +00:00
|
|
|
if ((p = Yap_GetExpPropHavingLock(ae, 1)) == NULL) {
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
2005-10-31 12:27:54 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
RepExpProp(p)->FOfEE.unary = InitUnTab[i].f;
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
}
|
2005-03-01 22:25:09 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|