/************************************************************************ * * * 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 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)) { case 0 : { fval = frexp(numval(op1), &exp0); follow(op2) = makefloat(fval); pushtrail(op2); follow(op3) = makeint((word)exp0); pushtrail(op3); break; } case 1 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) ); follow(op1) = makefloat(fval); pushtrail(op1); break; } case 3 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) ); 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); if (!unify(op1, makefloat(log((double)numval(op2))))) {Fail0;} break; } case 1 : { deref(op1); if (!unify(makefloat(exp((double)numval(op1))), op2)) {Fail0;} break; } case 2 : { deref(op2); if (!unify(op1, makefloat(sqrt((double)numval(op2))))) {Fail0;} break; } case 3 : { deref(op1); y = (double)numval(op1); if (!unify(makefloat(y * y), op2)) {Fail0;} break; } case 4 : { deref(op1); if (!unify(makefloat(sin((double)numval(op1))), op2)) {Fail0;} break; } case 5 : { deref(op2); if (!unify(op1, makefloat(asin((double)numval(op2))))) {Fail0;} break; } }; return; }