From: mmcg@cs.monash.edu.au (Mike  Mc Gaughey)
Subject: Re: MATH.C
Organization: Computer Science, Monash University, Australia
Date: Sat, 10 Oct 1992 08:16:17 GMT

"Michael L. Yacht" <my3k+@andrew.cmu.edu> writes:

>All i can say, is that whomever made the drivers for this 3.1.2 REALLY
>neglected some important things.  especially simple math funtions,
>like POWER(), SQRT(), SIN() COS(), I think it REALLY needs to be added
>in.  ESPECIALLY SQRT().  If anyone has a nice hack, that i could use,
>to get a SQRT() or some reasonable facismile thereof.  ALSO, what is
>the largest number I can stick into an int?  Is it the 32xxx number?
>If so, how can I get a numbertype that is larger?

Ok.  Ints on most machines are 32 bit; thus, the range of possible
ints is avout 2^32, and range from -2^31 to +2^31 - 1.  This means
maxint is a couple of billion.  If you're running off a PC, maxint
is probably 2^15 - 1, which is about 32767; you need to change the
'int' definition to 'long' to get the larger range (note: I'm assuming
that your 3.1.2 driver implements ints as an int + a seperate tag -
I've never examined the code.  If the tag is builtin to the int - possible,
though difficult to get it to work with everything - you'll have
a couple less bits to play with).

Math libraries are problematic.  2.5.4 or so contained an abysmal hack
(load average) that required the driver to be compiled -lm (i.e.
linked to the maths library), so there's not going to be much increase
in driver size from providing them - BUT:

(a) If yer gonna do it, you should do it properly.  This means adding
floating point as a basic LPC datatype.  I did it a while ago on
Shattered World; it didn't take long (a couple of hours to implement
it, and overload all the appropriate operators: +, -, ==, <, >, add it
to the parser and savefile parser, etc).  Note that a float on my
machine is the same size as an int, so there were no nasty decisions to
make about space usage.  I don't know whether 3.x has floats; if you
don't, guys, it's not hard to add.

(b) The maths library is implemented in C.  You need to call it from
LPC.  At the moment, the only way to do this is to provide efuns (not
simulated ones) to get to the library routines.  Most implementors
*hate* the idea of providing a swag of extra (real) efuns for access to
a little-used library (let's face it, floating point _is_ little
used).

As a data point: I've had floating point (in LPC) for about 5 months
now.  I still don't have efuns to get at the maths library (though I've
been asked for them; the only efuns we have are int2real and
real2int).  And I won't be providing any until I have set up a clean
way of doing it (this is in the works :).  So, while we _can_ do fancy
things like scale all our skills to between 0 and 1 (not that we have),
or deal with huge amounts of cash, we generally don't.  Integers are
still sufficient for almost everything :)

Ok - assuming you need larger range on your ints: it's simplest to 
write a module to deal with integer vectors (or something) and implement
addition, multiplication, etc, in LPC, as operations on these vectors.
You have multiple inheritance - you know what to do :)

Power(): consider a ^ b - Power(a, b).  You can write this as:
    a*a*a*a*...*a
    <- b times ->

This is the same as:
    a * a * a * ............ * a
    <- b/2 ->    <- b/2  -> < b % 2>  (i.e. 0 or 1 times)

i.e. power(a, b) = power(a, b/2)^2 + (b%2)*a

So, assuming b is a positive integer, you can do it in logarithmic
time (w.r.t. b) in LPC (with a loop - no need for recursion here).

Sqrt(): Again, you can write sqrt in LPC, and get quite decent speed
from it (using any textbook implementation).  I don't know the fastest
sqrt implementation (the ones I know off the top of my head are all
root-finding algorithms - gaussian, etc - there may be better ones).
Just find a numerical algorithms text, look it up and write it in LPC.
Then post the code :)

Sin, cos, etc: do you really need these?  What will you want next,
hyperbolic functions?  What for?

Cheers,

    Mike.
--
Mike McGaughey			AARNET:	mmcg@bruce.cs.monash.edu.au

"Head stompin', ass kickin', finger licking nastiness"
Newsgroups: rec.games.mud.lp