Tentative Guidelines for Numbers
Dar Scott
dsc at swcp.com
Mon Apr 1 16:28:01 EST 2002
To make up for any confusion I helped create and to help contribute
to easing the confusion about using Revolution numbers in general,
I offer these guidelines on using Revolution numbers. This is
based on my experience in computer representations of numbers and
only a brief exposure to Revolution.
Perhaps this can be a starting point that we can hack at to get
something appropriate.
1.
Signed whole numbers of up to 15 digits are represented exactly.
If results of multiplication, addition and subtraction on numbers
in this range are in this range, then the results are exact. Many
cases of division and exponentiation are exact. Some other
functions might give exact results. (This needs more exploration.
Stronger statements about subtraction and exponentiation may be
appropriate.)
Use numbers in this range if exact results are important. Be
careful not to corrupt your calculations with values outside this
range.
2.
Very large numbers (ignoring sign) or numbers with fractional parts
might not be represented exactly. Errors in math functions on such
numbers can accumulate. Also, adding a small number to a large
number can cause information about the small to be lost and in the
extreme case will not cause a change in the number.
If you are adding a large number of numbers, consider starting with
the smallest first.
Avoid using = when you can. Use > or < for boundary conditions
rather than =.
In loops use the "repeat with" form and derive values for each
iteration from the loop variable instead of incrementing numbers
with fractions.
Consider scaling arithmetic into a whole number range.
3.
Equality for numbers is tolerant of small differences.
Use = for numbers in 2 above only after a couple calculations.
Accumulated errors may go outside of what = will consider equal.
Use < or < before <= or >=. Use any before =. (Actually, I'm
guessing about <= or >=. Some tests need to be run to see if they
have the same tolerance as =.)
4.
The underlying number representation is carried along in
expressions and variables. A value might behave one way when used
as a number and other when used as a string (sequence of
characters). Not that ( n & empty ) will force representation to a
string and apply numberFormat. For some values, ( n & empty) = n
is false. If you can see a number (as a numeral), it has been
converted to a string.
You can use ( n & empty ) to round as defined in numberFormat. Use
this, round() and truncate() as need when working with numbers in
#2 above.
5.
Some fractional numbers are exact, such as 0.5.
Take advantage of this.
6.
The range for bit functions and base conversion is more restricted
than that in #1 above.
7.
Very large and very exact numerals can be represented exactly as
strings, of course. However, the results of math functions on
those will be based on values with the limitations described above.
I hope this helps. It needs corrections, additions,
clarifications, detailing, wisecracks...
Dar Scott
More information about the use-livecode
mailing list