Mastering E-Prime: Rounding, truncation, floor, ceil

939 views
Skip to first unread message

David McFarlane

unread,
May 17, 2011, 12:36:48 PM5/17/11
to e-p...@googlegroups.com
E-Prime (i.e., VBA) has no functions named "trunc()", "floor()", or
"ceil()", but as a point of reference we recognize the following
standard definitions:

- trunc(x) = integer part of x, i.e., x rounded toward 0.
Note that, e.g., trunc(2.1) = 2, and trunc(-2.1) = -2.
- floor(x) = nearest integer less than or equal to x.
Note that, e.g., floor(2.1) = 2, while floor(-2.1) = -3.
- ceil(x) = nearest integer greater than or equal to x.
Note that, e.g., ceil(2.1) = 3, while ceil(-2.1) = -2.

Now, E-Prime does provide the following:

- Fix(x) "returns the integer part of x", i.e., it simply truncates x
(trunc(x)), i.e., it rounds toward 0.
- Int(x) "returns the integer part of a given value by returning the
first integer less than the number", i.e., does a floor(x). (Yes,
PST's using "integer part" in both descriptions does confuse things.)
- CInt(x) returns an integer for the round value of x (using "bankers'"
or "scientific" rounding, where values ending in .5 get rounded to the
the nearest even integer).
- CLng(x) does the same as CInt(x) but returns a Long.
- Format$(x) returns a String (or, Format(x) a String Variant) of x
according to an optional format specifier string, and may round x
(using "bankers'" or "scientific" rounding).
- x\y does integer division after first rounding the arguments (using
"bankers'" or "scientific" rounding), i.e.,
(x\y) = Fix( CInt(x) / CInt(y) ).

Using these, we may implement ceil(x) with -Int(-x).

-- David McFarlane, Professional Faultfinder

Reply all
Reply to author
Forward
0 new messages