Qbasicnews.com
February 22, 2020, 07:27:16 AM
 Pages: 1 2 3 [4] 5 6 7
 Author Topic: Challenge: Algorithms having only one line of code.  (Read 34945 times)
oracle
*/-\*

Posts: 3652

 « Reply #45 on: June 30, 2003, 01:42:24 AM »

I already made a function to do that for statlib... pity I'll have to rewrite it because I'm using strings now...

Functions for finding roots of quadratic equations. Two needed: one for each root.

Code:
a = 1
b = 4
c = 2
' equivilant to x²+4x+2

r1 = (-b + (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
r2 = (-b - (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
PRINT r1, r2
' returns -1, -3
 Logged

ak00ma
Ancient Guru

Posts: 669

 « Reply #46 on: June 30, 2003, 12:35:55 PM »

I only need a sine algorithm that uses degrees and not radians...then I'll code it
 Logged

B 4 EVER
DrV
Na_th_an

Posts: 1553

 « Reply #47 on: July 01, 2003, 01:33:50 AM »

Quote from: "oracle"
I already made a function to do that for statlib... pity I'll have to rewrite it because I'm using strings now...

Functions for finding roots of quadratic equations. Two needed: one for each root.

Code:
a = 1
b = 4
c = 2
' equivilant to x²+4x+2

r1 = (-b + (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
r2 = (-b - (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
PRINT r1, r2
' returns -1, -3

That only finds the real roots... I suppose we'll let you get away with it this time...
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #48 on: July 01, 2003, 03:30:54 AM »

I know that... *shoots DrV*. How d'you expect me to find complex roots, or even represent them in QB? I suppose it could be done using the symmetrical property of the roots and polar form, but then it wouldn't be one line.
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #49 on: July 01, 2003, 06:42:37 PM »

Here's a rather complex one-liner to determine if a given number N is a power of 2.
Code:
defLNG a-z

if 2^(int(log(N)/log(2)+.5)) = N then print n;" is a power of 2"

Note: My original code did not add the .5 to round the result. However, further testing showed that for certain values of N, like 128, the arithmetic failed. Can anyone tell me why 128 fails without the rounding?
*****
 Logged
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #50 on: July 01, 2003, 06:53:41 PM »

Adding .5 before INT does round to the next integer, the same as CINT does. INT is a floor function. Maybe your original code is in another language?
 Logged

Antoni
Moneo
Na_th_an

Posts: 1971

 « Reply #51 on: July 01, 2003, 07:03:43 PM »

Yes, the original code was in PICKBasic, which is identical for this one line of code.

What do you mean by a "floor function"?

Have you been able to determine why the formula fails for N=128 without the rounding?
*****
 Logged
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #52 on: July 01, 2003, 07:15:53 PM »

This way works with 128:
Code:

DEFLNG A-Z
a! = LOG(n) / LOG(2)
IF 2 ^ a! = n THEN PRINT n; " is a power of 2"

So i imagine it's a QB bug...

A floor function rounds towars the smaller integer.
 Logged

Antoni
na_th_an
*/-\*

Posts: 8244

 « Reply #53 on: July 01, 2003, 07:25:12 PM »

I use a replacement for this kind of instruction:

Code:
IF <condition> THEN
a = <value1>
ELSE
a = <value2>
END IF

The replacement goes as follows:

Code:
a = (<value1> AND <condition>) + (<value2> AND (NOT <condition>))

Workis well with integer data.
 Logged

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

Posts: 3652

 « Reply #54 on: July 01, 2003, 11:28:55 PM »

I've got a function for statlib somewhere that does correct rounding (eg .5 and up rounds up, and <.5 rounds down) because the QB ones wern't working for all decimal values. I'll try to find it...
 Logged

BinarySHOCK
Forum Regular

Posts: 117

 « Reply #55 on: July 02, 2003, 03:10:58 AM »

Here's a crappy rounding thingy

N = 3445    ' Number To Round
Dire = 0    ' Direction to round, 1 = Up, 0 = Down

Rounded = CINT(N + ((Dire = 0) - (Dire = 1)) * .5)

PRINT Rounded
 Logged

very F***ing song remains the same
To everyone who sucks-up for the fame
Out of strength you know we speak the truth
Every trend that dies is living proof

MasterMinds Software
whitetiger0990
__/--\__

Posts: 2964

 « Reply #56 on: July 02, 2003, 10:29:56 AM »

this rounds abd is 1 line of code minus the lines that define the variables.

Code:
dec = 1      'how many decimal places to round
num = 2.56   'number to round
PRINT INT(10 ^ dec * num + .5) / 10 ^ dec

This is actually ALauzier's but I just posted it
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Moneo
Na_th_an

Posts: 1971

 « Reply #57 on: July 02, 2003, 12:29:09 PM »

An excellent example of how a pure logic statement can replace an IF statement.
*****
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #58 on: July 02, 2003, 12:47:46 PM »

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)

*****
 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #59 on: July 02, 2003, 12:58:28 PM »

Code:
CLS
dec = 2
num = -2.7234
IF NUMBER < 0 THEN rfactor = -.5 ELSE rfactor = .5
PRINT INT(10 ^ dec * num + rfactor) / 10 ^ dec

there
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
 Pages: 1 2 3 [4] 5 6 7