I too prefer rounding of negative numbers towards plus infinity; to me, zero
should be merely a reference point, not a reflection point..  But to-integer
(n + .5) doesn't work the way I thought it should between -1.499999 and -.5
it yields 0 as it does from 0 to +.499999.  In other words, the range over
which it yields 0 is approx 2 there, where elsewhere the range is 1.  Round
can't be used to create a step function where the "treads" are all 1 in
"width".

The function below nicely rounds negative numbers towards minus infinity the
way engineers prefer, so it, too, results in a tread width of 2 around zero.

I've worked around this in several applications (e.g., locating a cursor in
a square of a game board) by first adding an arbitrary integer constant to
make all practical numbers positive, then rounding, then subtracting the
constant.

The problem is related to the fact that decimal fractions increase the
magnitude of the decimal number relative to its integer part, rather than
increase towards plus infinity.  Compare this with logarithms mantissa and
characteristic.  (I can't remember which is which!!:>) )
Russell [EMAIL PROTECTED]
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, November 20, 1999 8:02 AM
Subject: [REBOL] Round function Re:(4)


> >
> > How about this function for rounding positive or negative numbers:
> >
> > round: func [R [integer! decimal!]][
> >     to-integer R + (-1 * to-integer negative? R) + .5
> > ]
> >
> > Shows one of the lesser-known uses of logic values in REBOL :-)
>
> I still don't understand why should -1,5 (or by US syntax -1.5)
> be rounded to -2. I think it should be rounded to -1 and I also
> think that the definiton of rounding by  to-integer! (x + 0.5)
> is fully convenient.
>
> (I study math (also ;-) on university for more than 4 years... :-)
>
> Regards,
> Jan
>
> --
> Jan Strejcek
> [EMAIL PROTECTED]
>

Reply via email to