Node: Arithmetic Operations, Next: Component Extractions on Numbers, Previous: Comparison on Numbers, Up: Numbers
Basic arithmetic can be done on Lpp Numbers with the following.
| plus x y | Function |
| minus x y | Function |
| times x y | Function |
| divide x y | Function |
In these functions x and y must each be Lpp Numbers or
int types and the returned result is a new Lpp Number. Their
names describe what they do. It it worth mentioning however that
divide does not truncate its value in any way, use the
truncate function for that See Component Extractions on Numbers. If divide is given two number that do not divide evenly it
will return an Lpp Ratio of the form x/y after it has been
rationally canonicalized See Number Types. For example
divide(L(12), L(4)) => 3 // evenly divides
divide(L(13), L(4)) => 13/4 // doesn't so returns ratio
divide(4, L(12)) => 1/3 // returns canonicalized ratio
truncate(L(13), L(4)) => 3 // Use truncate function to truncate
| inc x | Function |
| inc x delta | Function |
| dec x | Function |
| dec x delta | Function |
These functions take an Lpp Number x and increments it in the
case of inc and decrements it in the case of dec. The
argument x is returned with the new value. Normally the
increment or decrement is by 1, however if an optional second argument
delta is given it must be an Lpp Number or int and the
increment or decrement is by delta instead of 1.
Both inc and dec modify the object x so that most
of the time a new object is not created. However there are cases
where the a new object is created. This is when there is an object
transition, for example from a SmallInteger to a BigInteger
See Number Types. So in the following
inc(x, 1024); // OK but ...
x = inc(x, 1024); // this hadles object transition cases
if there is no transition expected then the first is fine. If a transition is possible then the second must be used and is the recommended form most of the time.
When there is an object transition Lpp automatically garbage collects
the defunct object and returns the new incremented or decremented
object. So in this case if the second code line above were not used
then the new object would be lost and becomes a potential memory leak
and x would point to deallocated memory.
| negate x | Function |
The negate function simply negates the Lpp number object that
x points to. While this is similar to inc and
dec in that it modified the object x, the object
x is never transitioned to another type of object and hence
does not have the transitioning concern. In the following
negate(num); // Always OK
num = negate(num); // Does the same thing, but unnecessary
other = negate(num);
the first is always safe to do and recommended, however the second
works also but in unnecessary. The third would both negate num
and set other to the negated num.
| gcd x y | Function |
The gcd function returns the greatest common divisor of
x and y each of which must be an Lpp Integer or
int type. The result is always a positive Lpp Integer. Here
are some examples
gcd(108, L(117)) => 9
gcd(L(123), -48) => 3
gcd(gcd(a, b), c); // Returns the gcd of a b and c