Subject: Fractions?

From: "R.Mehta" <[email-masked]>
Date: Wed, 30 Jun 1993 05:07:53 -0700

Hi,

I  was wondering if any one knows how to use fractions in MOO  or
at least how to work out a percentage?

Thanx

---

From: Peter <[email-masked]>
Date: Wed, 30 Jun 1993 08:21:32 -0700

I remember seeing once a tutorial for how to build a wind-up duck.  Does
anyone know where I can find this?  How about other tutorial-type things?

Thanks in advance?

                       [gcc]                                     [g++]
/*--------------------------            // ---------------------------
  Peter (Z-man) Zatloukal               //  Peter (Z-man) Zatloukal
  NeXT Lab Administrator                //  ACC Lead Lab Assistant
  [email-masked]             //  [email-masked]
  --------------------------*/          // ---------------------------

---

From: Judy Anderson <[email-masked]>
Date: Wed, 30 Jun 1993 08:40:41 -0700

The tutorial, along with a vast quantity of other documentation, is
available on /pub/MOO/ and /pub/MOO/contrib/ on parcftp.xerox.com.

---

From: [email-masked]
Date: Wed, 30 Jun 1993 09:08:08 -0700

> at least how to work out a percentage?

The problem is that MOO only handles integer arithmetic, although I am given
to believe that may one day change.  In the meantime you have 2 choices:

1) Code a division method using only integer arithmetic.

   This is the tack taken in $math_utils, where a number of functions exist.
   They tend to scale the result by some large number, say 10000: 

     help $math_utils:sin
     Information about Math Utilities(#3635):sin
     ----
     sin(x) -- given x in degrees, sin(x) will return the value of the sine
     function at x times 10000

   I don't think there is a similar :ratio function, but it would be trivial
   to install:

     "ratio(numerator,denominator) -- returns ratio times 10000, beware of
     using numbers with absolute values greater than $maxint/10000!";
     numerator = args[1]*10000;
     denominator = args[2];
     return $math_utils:div(numerator,denominator);

   Then all you have to do is remember that it returns 10000 times the ratio,
   so a return value of 5000 should be interpreted as 1/2.  Another way
   to code this would be to use string representations of numbers and
   just code up the old long division algorithm you learned in grade school.
   That would be significantly harder (but more fun), and would allow
   arbitrary precision.

2) Install a floating point math server

   This is what we did on AstroVR to do floating point arithmetic.  In this
   method, numbers are represented by their string values, ie "3.14159" etc.
   We installed a bc server that we can telnet into using the built-in
   open_network_connection and then send a list of strings representing the
   bc program.  Reading off the same connection we get the output.

     look bc
     This is the bc calculator object. It connects to a bc server to do 
     floating point and other heavy-duty arithmetic calculations.  See 
     'man bc' for the unix man page on the bc language, which resembles c.  
     To use bc in a program, include the line

        bc_output=$bc:eval(bc_code);

     where bc_code is a list of strings containing the bc program, most 
     conveniently created by editing a property on some object.  The return 
     value is a list of strings containing the output.  Note that there is 
     about a second of overhead for each call to $bc:eval, so do calculations 
     in as big chunks as you can.

     Example:

       ;player:tell_lines($bc:eval({"{","scale=3","a=1.0",
                                   "b=sqrt(2)","a+b","}"}));
       2.414
       => 0
       [used 14999 ticks, 11 seconds.]

  Note: This actually only took about 1 second of real time, so I don't
  know why it thinks it took 11 seconds!

  If you go this route remember that all numbers are now represented by
  strings, but now you can handle any number that bc can handle, which means
  that you can do just about anything.  Of course now you can't just do
  ratios, but you have access to all the other bc functions too.

This was probably more detail than you wanted, but it is probably useful to
others on the list as well.

Cheers!

 -- Dave Van Buren