Qbasicnews.com
September 19, 2019, 03:54:51 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 ... 3 4 [5] 6 7
  Print  
Author Topic: Challenge: Algorithms having only one line of code.  (Read 33667 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #60 on: March 19, 2006, 10:19:57 PM »

Quote from: "Moneo"
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)

*****
Logged
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #61 on: March 20, 2006, 02:09:35 AM »

Why couldn't you just do this?

Code:
Function Round( n As Single ) As Integer
    Round = n\1
End Function
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #62 on: March 20, 2006, 05:04:40 PM »

Quote from: "Dr_Davenstein"
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.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #63 on: March 20, 2006, 05:20:36 PM »

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.
*****
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #64 on: March 20, 2006, 07:24:02 PM »

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
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #65 on: March 21, 2006, 12:07:37 AM »

Quote from: "Moneo"
Quote from: "Dr_Davenstein"
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!
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #66 on: March 21, 2006, 12:10:08 AM »

Quote from: "Ralph"
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.
*****
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #67 on: March 21, 2006, 12:19:04 AM »

Quote from: "Moneo"
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?
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #68 on: March 22, 2006, 01:35:33 AM »

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.
*****
Logged
Nemesis
Forum Regular
**
Posts: 118



« Reply #69 on: March 27, 2006, 10:11:59 AM »

'
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.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #70 on: March 27, 2006, 04:55:38 PM »

Quote from: "Nemesis"
'
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.


That looks like a cool algorithm, EXCEPT it is not on only one line of code, as per the original rules for this thread, which stated:

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

The idea of one line algorithms is that they're simple, most people can immediately understand them, they serve as an example for similar simple logic, and people can later use them in their programs when needed.
*****
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #71 on: March 28, 2006, 12:05:18 AM »

Moneo:

Here's another one-liner to turn a switch on and off, in accordance with, and I quote you, "Most programmers consider TRUE as -1, and FALSE as 0."

x = -1 - x

Starting with x = 0, the next time it encounters the above, the new value will be, x = - 1, then, x = 0, then, x = -1, and so on...

In other words, as long as the previous value of x is 0 or -1, the one-liner will toggle the value of x to either -1 or 0, etc.
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #72 on: March 28, 2006, 11:02:38 AM »

Quote from: "Moneo"
Quote from: "Ralph"
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.
*****


Code:
A = 1 - A
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #73 on: March 28, 2006, 07:30:32 PM »

Did someone already post this one?

Code:
A = NOT A
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #74 on: March 28, 2006, 09:56:04 PM »

Quote from: "na_th_an"
Quote from: "Moneo"
Quote from: "Ralph"
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.
*****


Code:
A = 1 - A


Yeah, Nathan, that's very good. But we should always define that this algorithm will flip a switch which is either 0 or 1. BTW I forgot to mention this in the examples of the original rules of the thread.

NOTE: Speaking of those examples, the following code is incorrect for flipping a 0 or 1 switch called sw:
sw = sw xor sw
It should be:
sw = sw xor 1

I'm surprised nobody caught that.
*****
Logged
Pages: 1 ... 3 4 [5] 6 7
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!