Qbasicnews.com

Full Version: Challenge: Algorithms having only one line of code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10
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)

*****
Why couldn't you just do this?

Code:
Function Round( n As Single ) As Integer
    Round = n\1
End Function
Quote:Why couldn't you just do this?

Code:
Function Round( n As Single ) As Integer
    Round = n\1
End Function

I've never tried your approach using integer division. The manual says that the variable is first "rounded" to an integer or long before the divide. That's great, but which of the 8 Microsoft defined rounding methods does it perform?

Give me a chance to test it on my DOS machine, and I'll get back to you.
*****
In my post of yesterday, I stated the following:
" .... I discovered that using a rounding factor of .5 or -.5 does not work 100% for reasons unknown."

Well, I wrote a little test program and discovered that the INT function in QB produces different results depending on the sign of the variable. An INT(-123.9) works differently than INT(123.9).

Since my original algorithm maintains the sign of the variable and uses a positive or negative rounding factor, then the INT with negative numbers does not always work like you would assume.

To appreciate this, you have to do some testing with the same positive and negative numbers with decimal values of .0, .4, .5, .6, and .9.
*****
In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify?
Code:
IF A = 1 THEN A = 0 ELSE A = 1
Quote:
Dr_Davenstein Wrote:Why couldn't you just do this?

Code:
Function Round( n As Single ) As Integer
    Round = n\1
End Function

I've never tried your approach using integer division. The manual says that the variable is first "rounded" to an integer or long before the divide. That's great, but which of the 8 Microsoft defined rounding methods does it perform?

Give me a chance to test it on my DOS machine, and I'll get back to you.
*****

I did some testing. The n\1 yields mostly good rounding EXCEPT when the the number n is even and has a .5 decimal value. This rounding method is called Banker's Rounding. The one which we all use and which we are implementing here is Symmetric Arithmetic Rounding.

Example: 124.5 rounded should give 125. The n\1 approach gives 124.

Nice try!
*****
Quote:In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify?
Code:
IF A = 1 THEN A = 0 ELSE A = 1
Yeah, that works Ralph, but we have been considering as algorithms that code which does not have any IF's. That is, strictly computational, either arithmetic or logical.
*****
Quote:
Code:
Ok, here's the rules:

1) It must be considered an algorithm, formula or method.
    Add 15 to X   ----   X=X+15    This is not an algorithm, agreed?
    If it were, then every line of code in a program would be.

2) You must describe the algorithm first, i.e., what is its purpose.

3) Then show the one line of code.

4) Multiple statments on one line; like: x=x+1 : x=x and 15
    are not considered as one line of code.

A good example is one we've already seen with the XOR.
The purpose is to turn a switch on if it's off, and turn it off if it's on.
The one line of code is: sw=sw xor sw
or equally cool:              sw=1-sw
*****
Now, I quote once more, your second to last line, above, as:
Quote:The purpose is to turn a switch on if it's off, and turn it off if it's on.
Which is exactly what the one-liner I submitted does. Or, have I mis-read your original rules?
Ok, Ralph, we'll allow your one liner with an IF THEN ELSE.

This issue came up in the past. I personally don't consider a statement with an IF to be an algorithm, but some people do.

So now, try to come up with some other algorithms using only one line of code.
*****
'
coloums% = 6: rows% = 42:odds& = 1:FOR x = 1 TO coloums: odds& = odds& * ((rows + 1) - x) / x:NEXT:CLS : PRINT odds&

This is an algorithm for finding the odds of a lottery wheel.
Pages: 1 2 3 4 5 6 7 8 9 10