HyperCard: Round, Trunc, and Random Functions (7/92)

This article has two sections:
* The Round and Trunc Functions
* The Random Function

(A related Tech Info Library article describes how HyperCard does Math. It
deals with SANE, floating-point numbers, NaNs and INFs, and mixing reals
and integers.)

The round and trunc Functions
-----------------------------
The round and trunc HyperTalk functions have a long integer range. If
passed a number outside this range �? less than -231 or greater than 231 - 1
�? these functions will return ð2147483648 (i.e., -231 ).

The trunc function rounds toward zero, so that any fractional part is
removed. Examples: trunc(2.2) -> 2 ; trunc(-2.2) -> -2.

The round function in HyperCard rounds "to nearest" (see the Apple Numerics
Manual ). (It is also known as a statistical round.) "To nearest" refers to
a number's binary representation. This method is not the usual "round" that
we learn in school. Whenever a number is exactly halfway between integers
(e.g., 2.5), the "usual" round will round up. The round will round toward
the even integer, e.g., round(2.5) -> 2. NOTE: Scripters may notice
unexpected results when the numberFormat is imposed upon an expression.
Similar rounding occurs in this instance (and it's harder to predict the
rounding direction when dealing with fractions).

The random Function
-------------------
The random function in HyperTalk produces pseudo-random numbers. In
HyperCard 1.x, the random function had an integer (e.g., 215 - 1) range. In
HyperCard 2.x, this was extended to a long integer range. (It calls SANE
directly �? using RandomX. ) If passed an argument outside its range, the
random function will use the maximum instead.

If a non-integer number is passed to random, HyperCard effectively rounds
up before returning a random number. If a number less than 1 is passed to
random, HyperCard returns 1.


This article is adapted from the Claris Tech Info database.
Published Date: Feb 19, 2012