home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
pd3.lzh
/
SBPROLOG2.2
/
SIM
/
BUILTIN
/
arith.c
next >
Wrap
Text File
|
1991-08-10
|
5KB
|
177 lines
/************************************************************************
* *
* The SB-Prolog System *
* Copyright SUNY at Stony Brook, 1986 *
* *
************************************************************************/
/*-----------------------------------------------------------------
SB-Prolog is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the SB-Prolog General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
SB-Prolog, but only under the conditions described in the
SB-Prolog General Public License. A copy of this license is
supposed to have been given to you along with SB-Prolog so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
------------------------------------------------------------------ */
/* some builtins for arithmetic functions */
#include "builtin.h"
extern double ldexp(), frexp();
extern double floatval();
extern word makefloat();
extern prettymuch_equal();
b_FLOATC()
{ /* F, M, E, S : F is either a variable or a number (in WAM format);
M is either a number (in WAM format) or a variable; E is either
an integer or a variable. The intended interpretation is that
M and E represent the mantissa and exponent, respectively, of
the floating point number F. Either F, or both M and E, are
assumed to be bound (the selector S tells us which case it is),
and no checking for this is done here. */
register word op1, op2, op3, op4;
register pw top;
double fval; int exp0;
op1 = gregc(1); deref(op1);
op2 = gregc(2); deref(op2);
op3 = gregc(3); deref(op3);
op4 = gregc(4); deref(op4);
switch ((int)intval(op4)) {
#ifndef OS9
case 0 : { fval = frexp(numval(op1), &exp0);
#else
case 0 : {
if (isinteger(op1))
fval = frexp((double)intval(op1), &exp0);
else fval = frexp(floatval(op1), &exp0);
#endif
follow(op2) = makefloat(fval); pushtrail(op2);
follow(op3) = makeint((word)exp0); pushtrail(op3);
break;
}
#ifndef OS9
case 1 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
#else
case 1 : {
if (isinteger(op2))
fval = ldexp((double)intval(op2), (int)intval(op3));
else fval = ldexp(floatval(op2), (int)intval(op3));
#endif
follow(op1) = makefloat(fval); pushtrail(op1);
break;
}
#ifndef OS9
case 3 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
#else
case 3 : {
if (isinteger(op2))
fval = ldexp((double)intval(op2), (int)intval(op3));
else fval = ldexp(floatval(op2), (int)intval(op3));
#endif
if (!prettymuch_equal(floatval(op1), fval))
{Fail0; break;}
}
};
return;
}
b_ARITH()
{
register word op1, op2, op3;
register pw top;
double y;
op1 = gregc(1);
op2 = gregc(2);
op3 = gregc(3); deref(op3);
switch ((int)intval(op3)) {
case 0 : { deref(op2);
#ifndef OS9
if (!unify(op1, makefloat(log((double)numval(op2)))))
#else
if ((isinteger(op2) &&
(!unify(op1, makefloat(log((double)intval(op2)))))) ||
(isfloat(op2) &&
(!unify(op1, makefloat(log((double)floatval(op2)))))))
#endif
{Fail0;}
break;
}
case 1 : { deref(op1);
#ifndef OS9
if (!unify(makefloat(exp((double)numval(op1))), op2))
#else
if (isinteger(op1) &&
(!unify(op2, makefloat(exp((double)intval(op1))))))
{Fail0;}
else if (isfloat(op1) &&
(!unify(op2, makefloat(exp((double)floatval(op1))))))
#endif
{Fail0;}
break;
}
case 2 : { deref(op2);
#ifndef OS9
if (!unify(op1, makefloat(sqrt((double)numval(op2)))))
#else
if ((isinteger(op2) &&
(!unify(op1, makefloat(sqrt((double)intval(op2)))))) ||
(isfloat(op2) &&
(!unify(op1, makefloat(sqrt((double)floatval(op2)))))))
#endif
{Fail0;}
break;
}
case 3 : { deref(op1);
#ifndef OS9
y = (double)numval(op1);
#else
if (isinteger(op1))
y = (double)intval(op1);
else y = floatval(op1);
#endif
if (!unify(makefloat(y * y), op2)) {Fail0;}
break;
}
case 4 : { deref(op1);
#ifndef OS9
if (!unify(makefloat(sin((double)numval(op1))), op2))
#else
if ((isinteger(op1) &&
(!unify(op2, makefloat(sin((double)intval(op1)))))) ||
(isfloat(op1) &&
(!unify(op2, makefloat(sin((double)floatval(op1)))))))
#endif
{Fail0;}
break;
}
case 5 : { deref(op2);
#ifndef OS9
if (!unify(op1, makefloat(asin((double)numval(op2)))))
#else
if ((isinteger(op2) &&
(!unify(op1, makefloat(asin((double)intval(op2)))))) ||
(isfloat(op2) &&
(!unify(op1, makefloat(asin((double)floatval(op2)))))))
#endif
{Fail0;}
break;
}
};
return;
}