Understanding code

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

Understanding code

Post by LEGRAND »

Looking at a program for astronomy, I find the following line:

A= ATN2(SIN(L1(0))*COS(E) , COS(L1(0))) MOD 360

Can somebody knows what this comma means, right in the middle, and how the line should be written in correct qbasic style?
I must say this program is from a 20 years old book and it is not pure basic.
I thought to replace it (the comma) by / but doesn't give the correct answer for A .
Thanks for help
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

ATN2

Post by burger2227 »

The following code is from VB, but can be used in QB.

Code: Select all

Atn2 - Arc tangent of Y/X
' arc tangent of Y/X - returns values in all four quadrants

Function Atn2(x As Double, y As Double) As Double
    If x = 0 Then
        Atn2 = Sgn(y) * 1.5707963267949
    ElseIf x > 0 Then
        Atn2 = Atn(y / x)
    Else
        Atn2 = Atn(y / x) + 3.14159265358979 * Sgn(y)
    End If
End Function
ALSO. The value of pi = 4 * ATN(1#) . Returns the double value so you can substitute the value easily in a program without having to remember it.

I found this on the web. Try looking there for code conversions.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Your
A= ATN2(SIN(L1(0))*COS(E) , COS(L1(0))) MOD 360

seems to me to be the same as
A= ATN(SIN(L1(0))*COS(E) / COS(L1(0))) 'radians, or
A= 45/ATN(1) * ATN(SIN(L1(0))*COS(E) / COS(L1(0))) 'degrees
where the "A" means "Angle"

Your ATN2() seems to be a SUB, which simply does the division of the first part over the second part.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

SEEMS? It is an actual Function!

Post by burger2227 »

NOT my SUB? LOL, I found that FUNCTION and you are correct except for the first instance in the IF statements.

Naturally, ATN can be used to compare Y/X to determine the Radian angles. Zero could create division problems too. So the first IF avoids a "Division by zero" error.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Absolutely! I just tried to respond to your post, as assuming too much will usually just produce more problems. for instance, before the ATN2() function, there might be code that tests for non-zero of the denominator, or the function might do that. Who knows?
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

No, the FUNCTION checks for that, but the code to determine X and Y, or whatever you want to call them, still needs the other code to determine the values before the Function will work properly.

It could possibly all be all done inside the Function, if you wish.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

UNDERSTANDING CODE

Post by LEGRAND »

Thanks for your help.
Never seen that before. I wander what language it is. Fortran?

I also noted this one:

D0 = FNH(D): A0= FNH(A/15)
nowhere you can find DEF FNH
Basic refuses this direct using of FNH and also refuses a variable
For instance:
H being an input, N previously calculated, the program uses
N= N + 1721028.5+FND(H)/24
also refused by basic
Looks it has been written for one of those former computer and apparently the language somewhat changed
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

They gotta be somewhere

Post by burger2227 »

Those could just be other Functions using DEF FN. This format is still useable in QB. Probably GWBasic if it has line numbers.

Code: Select all

DEF FNH (parameters)

'Function's code 

END DEF
In the program code after the function, you just use FNH(parameters)
You may have to place the value into another variable like:
calc1 = FNH(parameters)

Also I Found a small DEF FN :

Code: Select all

 DEF FNH(Z)=(INT(RND(0)*Z)+1)*100  'defined in a game 
The above DEF FN is just one line of code, so no END DEF is used


You could also convert a DEF FN to a real function by using FUNCTION and END FUNCTION and remove the FN part in the code. DEF FN cannot be used more than once in a program for any of the Functions! So they can not be redefined later or an error will occur.

Ted
Last edited by burger2227 on Thu Jul 31, 2008 2:24 pm, edited 3 times in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Re: UNDERSTANDING CODE

Post by Ralph »

LEGRAND wrote:Thanks for your help.
Never seen that before. I wander what language it is. Fortran?

I also noted this one:

D0 = FNH(D): A0= FNH(A/15)
nowhere you can find DEF FNH
Basic refuses this direct using of FNH and also refuses a variable
For instance:
H being an input, N previously calculated, the program uses
N= N + 1721028.5+FND(H)/24
also refused by basic
Looks it has been written for one of those former computer and apparently the language somewhat changed
It seems to me that FNH(D) would be a function to convert days to hours, and that FND(H)/24 seems to be converting hours to days? But, who knows? I don't.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Post Reply