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 : eval . c *
* Last rev : *
* mods : *
* comments : arithmetical expression evaluation *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifdef SCCS
static char SccsId [ ] = " %W% %G% " ;
# endif
2014-04-21 11:14:18 +01:00
/**
@ file arith0 . c
2015-01-04 23:58:23 +00:00
*/
//! @{
/**
2014-04-21 11:14:18 +01:00
@ defgroup arithmetic_operators Arithmetic Functions
@ ingroup arithmetic
2014-05-06 13:58:08 +01:00
YAP implements several arithmetic functions , they are defined as
fields in three enumerations , such that there is one enumeration
per each different arity :
- # arith0_op defines constants and arity 0 arithmetic functions
@ copydoc # arith0_op
- # arith1_op defines single argument arithmetic functions
@ copydoc # arith1_op
- # arith2_op defines binary arithmetic functions
@ copydoc # arith2_op
Arithmetic expressions
2014-04-21 11:14:18 +01:00
in YAP may use the following operators :
- < b > pi [ ISO ] < / b > < p > @ anchor pi_0
An approximation to the value of < em > pi < / em > , that is , the ratio of a circle ' s circumference to its diameter .
- < b > e < / b > < p > @ anchor e_0
Euler ' s number , the base of the natural logarithms .
- < b > epsilon < / b > < p > @ anchor epsilon_0
The difference between the float ` 1.0 ` and the next largest floating point number .
- < b > inf < / b > < p > @ anchor inf_0
Infinity according to the IEEE Floating - Point standard . Note that evaluating this term will generate a domain error in the ` iso ` language mode .
Note also that YAP supports ` + inf ` and ` - inf `
- < b > nan ( not a number ) < / b > < p > @ anchor nan_0
Not - a - number according to the IEEE Floating - Point standard . Note that evaluating this term will generate a domain error in the ` iso ` language mode .
- < b > random < / b > < p > @ anchor random_0
A " random " floating point number between 0 and 1.
- < b > cputime < / b > < p > @ anchor cputime_0
CPU time since YAP was invoked , in seconds .
- < b > heapused < / b > < p > @ anchor heapused_0
Heap ( data - base ) space used , in bytes .
- < b > local < / b > < p > @ anchor local_0
Local stack in use , in bytes
- < b > $ b < / b > < p > @ anchor b_0
current choicepoint
- < b > $ env < / b > < p > @ anchor env_0
Environment
- < b > $ tr < / b > < p > @ anchor tr_0
Trail in use
- < b > $ free_stack < / b > < p > @ anchor free_stack_0
Amount of free stack space , that is , free space between global and local stacks .
- < b > global < / b > < p > @ anchor global_0
Global stack in use , in bytes .
2001-04-09 20:54:03 +01:00
*
*/
# 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"
2009-10-20 09:21:59 +01:00
2008-12-04 23:33:32 +00:00
static Term
eval0 ( Int fi ) {
2011-03-07 16:02:55 +00:00
CACHE_REGS
2008-12-04 23:33:32 +00:00
arith0_op fop = fi ;
switch ( fop ) {
case op_pi :
{
RFLOAT ( PI ) ;
}
case op_e :
{
RFLOAT ( M_E ) ;
}
2009-10-20 09:21:59 +01:00
case op_epsilon :
{
RFLOAT ( DBL_EPSILON ) ;
}
2008-12-04 23:33:32 +00:00
case op_inf :
{
2001-04-09 20:54:03 +01:00
# ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */
2014-10-16 10:49:11 +01:00
Yap_ArithError ( TYPE_ERROR_EVALUABLE , TermNil , " evaluating infinity " ) ;
2008-12-04 23:33:32 +00:00
P = ( yamop * ) FAILCODE ;
RERROR ( ) ;
2001-04-09 20:54:03 +01:00
# else
2015-06-18 00:18:28 +01:00
if ( isoLanguageFlag ( ) ) { /* iso */
2014-10-16 10:49:11 +01:00
Yap_ArithError ( TYPE_ERROR_EVALUABLE , TermNil , " evaluating infinity " ) ;
2008-12-04 23:33:32 +00:00
P = ( yamop * ) FAILCODE ;
RERROR ( ) ;
} else {
RFLOAT ( INFINITY ) ;
}
2001-04-09 20:54:03 +01:00
# endif
2008-12-04 23:33:32 +00:00
}
case op_nan :
{
2014-04-21 11:14:18 +01:00
# ifdef _MSC_VER /* Microsoft's Visual C++ Compi<ler */
2014-10-16 10:49:11 +01:00
Yap_ArithError ( TYPE_ERROR_EVALUABLE , TermNil , " evaluating infinity " ) ;
2008-12-04 23:33:32 +00:00
RERROR ( ) ;
# else
2015-06-18 00:18:28 +01:00
if ( isoLanguageFlag ( ) ) { /* iso */
2014-10-16 10:49:11 +01:00
Yap_ArithError ( TYPE_ERROR_EVALUABLE , TermNil , " evaluating not-a-number " ) ;
2008-12-04 23:33:32 +00:00
RERROR ( ) ;
} else {
RFLOAT ( NAN ) ;
}
# endif
}
case op_random :
{
RFLOAT ( Yap_random ( ) ) ;
}
case op_cputime :
{
RFLOAT ( ( Float ) Yap_cputime ( ) / 1000.0 ) ;
}
case op_heapused :
2014-04-21 11:14:18 +01:00
/// - heapused
/// Heap (data-base) space used, in bytes.
///
2008-12-04 23:33:32 +00:00
RINT ( HeapUsed ) ;
case op_localsp :
2014-04-21 11:14:18 +01:00
/// - local
/// Local stack in use, in bytes
///
2011-03-30 15:32:59 +01:00
# if YAPOR_SBA
2008-12-04 23:33:32 +00:00
RINT ( ( Int ) ASP ) ;
2001-04-09 20:54:03 +01:00
# else
2008-12-04 23:33:32 +00:00
RINT ( LCL0 - ASP ) ;
2001-04-09 20:54:03 +01:00
# endif
2008-12-04 23:33:32 +00:00
case op_b :
2014-04-21 11:14:18 +01:00
/// - $b
/// current choicepoint
///
2011-03-30 15:32:59 +01:00
# if YAPOR_SBA
2008-12-04 23:33:32 +00:00
RINT ( ( Int ) B ) ;
2001-04-09 20:54:03 +01:00
# else
2008-12-04 23:33:32 +00:00
RINT ( LCL0 - ( CELL * ) B ) ;
2001-04-09 20:54:03 +01:00
# endif
2008-12-04 23:33:32 +00:00
case op_env :
2014-04-21 11:14:18 +01:00
/// - $env
/// Environment
///
2011-03-30 15:32:59 +01:00
# if YAPOR_SBA
2008-12-04 23:33:32 +00:00
RINT ( ( Int ) YENV ) ;
2001-12-17 18:31:11 +00:00
# else
2008-12-04 23:33:32 +00:00
RINT ( LCL0 - YENV ) ;
2002-01-07 06:28:04 +00:00
# endif
2008-12-04 23:33:32 +00:00
case op_tr :
2014-04-21 11:14:18 +01:00
/// - $tr
/// Trail in use
///
2011-03-30 15:32:59 +01:00
# if YAPOR_SBA
2008-12-04 23:33:32 +00:00
RINT ( TR ) ;
2002-01-07 06:28:04 +00:00
# else
2008-12-04 23:33:32 +00:00
RINT ( ( ( CELL * ) TR ) - LCL0 ) ;
2001-12-17 18:31:11 +00:00
# endif
2008-12-04 23:33:32 +00:00
case op_stackfree :
2014-04-21 11:14:18 +01:00
/// - $free_stack
///
/// Not-a-number according to the IEEE Floating-Point standard. Note that evaluating this term will generate a domain error in the `iso` language mode.
2014-01-19 21:15:05 +00:00
RINT ( Unsigned ( ASP ) - Unsigned ( HR ) ) ;
2008-12-04 23:33:32 +00:00
case op_globalsp :
2014-04-21 11:14:18 +01:00
/// - global
/// Global stack in use, in bytes.
///
2011-03-30 15:32:59 +01:00
# if YAPOR_SBA
2014-01-19 21:15:05 +00:00
RINT ( ( Int ) HR ) ;
2001-04-09 20:54:03 +01:00
# else
2014-01-19 21:15:05 +00:00
RINT ( HR - H0 ) ;
2001-04-09 20:54:03 +01:00
# endif
2008-12-04 23:33:32 +00:00
}
2014-04-21 11:14:18 +01:00
/// end of switch
2008-12-04 23:33:32 +00:00
RERROR ( ) ;
2001-04-09 20:54:03 +01:00
}
2008-12-04 23:33:32 +00:00
Term Yap_eval_atom ( Int f )
2001-04-09 20:54:03 +01:00
{
2008-12-04 23:33:32 +00:00
return eval0 ( f ) ;
2001-04-09 20:54:03 +01:00
}
typedef struct init_const_eval {
char * OpName ;
2008-12-04 23:33:32 +00:00
arith0_op f ;
2001-04-09 20:54:03 +01:00
} InitConstEntry ;
static InitConstEntry InitConstTab [ ] = {
2008-12-04 23:33:32 +00:00
{ " pi " , op_pi } ,
{ " e " , op_e } ,
2009-10-20 09:21:59 +01:00
{ " epsilon " , op_epsilon } ,
2008-12-04 23:33:32 +00:00
{ " inf " , op_inf } ,
{ " nan " , op_nan } ,
{ " random " , op_random } ,
{ " cputime " , op_cputime } ,
{ " heapused " , op_heapused } ,
{ " local_sp " , op_localsp } ,
{ " global_sp " , op_globalsp } ,
{ " $last_choice_pt " , op_b } ,
{ " $env " , op_env } ,
{ " $tr " , op_tr } ,
{ " stackfree " , op_stackfree } ,
2001-04-09 20:54:03 +01:00
} ;
void
2002-11-18 18:18:05 +00:00
Yap_InitConstExps ( void )
2001-04-09 20:54:03 +01:00
{
unsigned int i ;
ExpEntry * p ;
for ( i = 0 ; i < sizeof ( InitConstTab ) / sizeof ( InitConstEntry ) ; + + i ) {
2002-11-18 18:18:05 +00:00
AtomEntry * ae = RepAtom ( Yap_LookupAtom ( InitConstTab [ i ] . OpName ) ) ;
2005-03-01 22:25:09 +00:00
if ( ae = = NULL ) {
2014-10-16 10:49:11 +01:00
Yap_EvalError ( OUT_OF_HEAP_ERROR , TermNil , " at InitConstExps " ) ;
2005-03-01 22:25:09 +00:00
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 , 0 ) ) {
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 = 0 ;
p - > ENoOfEE = 0 ;
2008-12-04 23:33:32 +00:00
p - > FOfEE = InitConstTab [ i ] . f ;
2011-08-17 19:16:21 +01:00
AddPropToAtom ( ae , ( PropEntry * ) p ) ;
2001-04-09 20:54:03 +01:00
WRITE_UNLOCK ( ae - > ARWLock ) ;
}
}
/* 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_ReInitConstExps ( void )
2001-04-09 20:54:03 +01:00
{
2005-10-31 12:27:54 +00:00
return TRUE ;
2001-04-09 20:54:03 +01:00
}
2014-04-21 11:14:18 +01:00
/// @}