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 >
Text File  |  1991-08-10  |  5KB  |  177 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /*        some builtins for arithmetic functions            */
  26.  
  27. #include "builtin.h"
  28.  
  29. extern double ldexp(), frexp();
  30.  
  31. extern double floatval();
  32. extern word  makefloat();
  33. extern prettymuch_equal();
  34.     
  35. b_FLOATC()
  36. {        /* F, M, E, S : F is either a variable or a number (in WAM format);
  37.            M is either a number (in WAM format) or a variable; E is either
  38.            an integer or a variable.  The intended interpretation is that
  39.            M and E represent the mantissa and exponent, respectively, of
  40.            the floating point number F.  Either F, or both M and E, are
  41.            assumed to be bound (the selector S tells us which case it is),
  42.            and no checking for this is done here.            */
  43.  
  44.     register word op1, op2, op3, op4;
  45.     register pw top;
  46.     double fval; int exp0;
  47.  
  48.     op1 = gregc(1); deref(op1);
  49.     op2 = gregc(2); deref(op2);
  50.     op3 = gregc(3); deref(op3);
  51.     op4 = gregc(4); deref(op4);
  52.     switch ((int)intval(op4)) {
  53. #ifndef OS9
  54.     case 0 : { fval = frexp(numval(op1), &exp0);
  55. #else
  56.     case 0 : {
  57.                 if (isinteger(op1))
  58.                     fval = frexp((double)intval(op1), &exp0);
  59.                 else fval = frexp(floatval(op1), &exp0);
  60. #endif
  61.            follow(op2) = makefloat(fval); pushtrail(op2);
  62.            follow(op3) = makeint((word)exp0); pushtrail(op3);
  63.            break;
  64.          }
  65. #ifndef OS9
  66.     case 1 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
  67. #else
  68.     case 1 : {
  69.                 if (isinteger(op2))
  70.                     fval = ldexp((double)intval(op2), (int)intval(op3));
  71.                 else fval = ldexp(floatval(op2), (int)intval(op3));
  72. #endif
  73.            follow(op1) = makefloat(fval); pushtrail(op1);
  74.            break;
  75.          }
  76. #ifndef OS9
  77.     case 3 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
  78. #else
  79.     case 3 : {
  80.                 if (isinteger(op2))
  81.                     fval = ldexp((double)intval(op2), (int)intval(op3));
  82.                 else fval = ldexp(floatval(op2), (int)intval(op3));
  83. #endif
  84.            if (!prettymuch_equal(floatval(op1), fval))
  85.             {Fail0; break;}
  86.         }
  87.     };
  88.     return;
  89. }
  90.  
  91. b_ARITH()
  92. {
  93.     register word op1, op2, op3;
  94.     register pw top;
  95.     double y;
  96.  
  97.     op1 = gregc(1); 
  98.     op2 = gregc(2);
  99.     op3 = gregc(3); deref(op3);
  100.     switch ((int)intval(op3)) {
  101.     case 0 : {  deref(op2);
  102. #ifndef OS9
  103.             if (!unify(op1, makefloat(log((double)numval(op2)))))
  104. #else
  105.             if ((isinteger(op2) &&
  106.                 (!unify(op1, makefloat(log((double)intval(op2)))))) ||
  107.                 (isfloat(op2) &&
  108.                 (!unify(op1, makefloat(log((double)floatval(op2)))))))
  109. #endif
  110.             {Fail0;}
  111.             break;
  112.          }
  113.     case 1 : {  deref(op1);
  114. #ifndef OS9
  115.             if (!unify(makefloat(exp((double)numval(op1))), op2))
  116. #else
  117.             if (isinteger(op1) &&
  118.                 (!unify(op2, makefloat(exp((double)intval(op1))))))
  119.                 {Fail0;}
  120.             else if (isfloat(op1) &&
  121.                 (!unify(op2, makefloat(exp((double)floatval(op1))))))
  122. #endif
  123.             {Fail0;}
  124.             break;
  125.          }
  126.     case 2 : {  deref(op2);
  127. #ifndef OS9
  128.             if (!unify(op1, makefloat(sqrt((double)numval(op2)))))
  129. #else
  130.             if ((isinteger(op2) &&
  131.                 (!unify(op1, makefloat(sqrt((double)intval(op2)))))) ||
  132.                 (isfloat(op2) &&
  133.                 (!unify(op1, makefloat(sqrt((double)floatval(op2)))))))
  134. #endif
  135.             {Fail0;}
  136.             break;
  137.          }
  138.     case 3 : {  deref(op1);
  139. #ifndef OS9
  140.             y = (double)numval(op1);
  141. #else
  142.             if (isinteger(op1))
  143.                 y = (double)intval(op1);
  144.             else y = floatval(op1);
  145. #endif
  146.             if (!unify(makefloat(y * y), op2)) {Fail0;}
  147.             break;
  148.          }
  149.     case 4 : {  deref(op1);
  150. #ifndef OS9
  151.             if (!unify(makefloat(sin((double)numval(op1))), op2))
  152. #else
  153.             if ((isinteger(op1) &&
  154.                 (!unify(op2, makefloat(sin((double)intval(op1)))))) ||
  155.                 (isfloat(op1) &&
  156.                 (!unify(op2, makefloat(sin((double)floatval(op1)))))))
  157. #endif
  158.             {Fail0;}
  159.             break;
  160.          }
  161.     case 5 : {  deref(op2);
  162. #ifndef OS9
  163.             if (!unify(op1, makefloat(asin((double)numval(op2)))))
  164. #else
  165.             if ((isinteger(op2) &&
  166.                 (!unify(op1, makefloat(asin((double)intval(op2)))))) ||
  167.                 (isfloat(op2) &&
  168.                 (!unify(op1, makefloat(asin((double)floatval(op2)))))))
  169. #endif
  170.                 {Fail0;}
  171.             break;
  172.          }
  173.     };
  174.     return;
  175. }
  176.  
  177.