Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#61
Quote:Let's not forget that positive numbers are rounded UP using .5, and negative numbers are rounded DOWN using -.5.

So, the simplest way is to test the sign of the number to be rounded, and set the rounding factor to .5 or -.5 accordingly. Something like this:
Code:
IF NUMBER < 0 THEN RFACTOR=-.5 ELSE RFACTOR=.5
RESULT=INT(NUMBER+RFACTOR)
*****

Hey guys, about a year after posting the above rounding solution, I discovered that using a rounding factor of .5 or -.5 does not work 100% for reasons unknown.

However, during this time, I discovered a foolproof rounding algorithm by Oracle, which is described in detail in this extract from a tutorial I wrote for QBNZ. Sorry I didn't correct this old post sooner. See the one line rounding algorithm at the end.


6. ROUNDING NUMBERS (POSITIVE OR NEGATIVE):
CREDIT: Oracle of Qbasicnews and QBNZ.
The purpose is to round positive or negative numbers to the nearest whole
number. The standard convention of "half-adjusting" by a rounding factor of
.5 is used. Notice that positive numbers must be rounded up, and negative
numbers are rounded down, that is, to a greater negative number.

NOTE: If you do INT(123.9) you will get 123. However, if you do INT(-123.9)
you will get -124. This is the documented method of how the INT works
in QB, which in the case of the INT(-123.9), is NOT what we want,
since we want -123.

The following code takes the above conditions into consideration.
* The absolute or ABS assures that the number to work with is positive.
* This positive number is rounded by the rounding factor of "+.5".
* The integer value INT is taken on the above positive result.
* The sign of the original input number is restored by multiplying by the
sign or SGN which is 1 for positive, -1 for negative, and 0 for zero.

GIVEN:
* N is the number to be rounded. Obviously, since it can have decimals,
it must have a type declaration of single or double floating point.
We will define it as single (!) for this example.
* R will be the resultant rounded whole number.

R = SGN(N!) * INT(ABS(N!) + .5)

*****
Reply


Messages In This Thread
To Oracle: 2nd look at your code. - by Moneo - 06-24-2003, 09:07 PM
To Seph and Agamemnus: - by Moneo - 06-24-2003, 09:17 PM
factorial. - by Agamemnus - 06-24-2003, 09:51 PM
To Agamemnus - by Moneo - 06-24-2003, 10:21 PM
To na_th_an: - by Moneo - 06-27-2003, 03:15 AM
ok, - by Agamemnus - 06-27-2003, 05:17 PM
To Oracle re Statlib - by Moneo - 06-27-2003, 10:39 PM
Re: To na_th_an: - by na_th_an - 06-28-2003, 01:14 AM
To LooseCaboose: - by Moneo - 06-28-2003, 03:08 AM
Re: To Oracle re Statlib - by oracle - 06-28-2003, 05:50 AM
To Oracle: - by Moneo - 06-29-2003, 03:10 AM
To Ak00ma: - by Moneo - 06-30-2003, 03:39 AM
Power of 2 - by Moneo - 07-02-2003, 02:42 AM
To Antoni: - by Moneo - 07-02-2003, 03:03 AM
197 - by whitetiger0990 - 07-02-2003, 06:29 PM
To na_th_an re "replacement" logic: - by Moneo - 07-02-2003, 08:29 PM
Re ROUNDING using .5: - by Moneo - 07-02-2003, 08:47 PM
Re: Re ROUNDING using .5: - by Moneo - 03-20-2006, 06:19 AM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:09 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:24 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:52 PM
my one-liner - by neuro - 03-30-2006, 12:08 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)