PDA

View Full Version : three questions



christianssen
09-03-2010, 11:36
one more question to math things with thinbasic.


' Empty GUI script created on 03-09-2010 10:22:46 by denis (ThinAIR)

Uses "console", "math"

MsgBox 0, "my multiply result: " + mymath()

Function mymath() As Long
Dim a,b,c As Long
Dim pival As Integer

a = 5
b = 6
c = a*b

PrintL a + b


PrintL Sin(a)
'PrintL Pi(b) '-- doesn't work, why ?

PiVal = 4 * Atn(1)
PrintL "my pival result: " + pival

PrintL Log(b)
'PrintL c-a ' c(-a) '-- doesn't work, why ?
'Printl a + mymath() '--- works, but fast list cannot stop to see

Function = c

End Function

WaitKey

my questions I named in code example ;)

denis

ErosOlmi
09-03-2010, 13:52
Post moved to General forum because this in an inquiry and not a code/snipet


[@]pi is a reserved keyword of MATH module: http://www.thinbasic.com/public/products/thinBasic/help/html/pi.htm
[@]PrintL accept strings as parameters and not numeric calculations so if you want make a calculation just use a formatting function like PrintL Format$(c - a)
[@]with "PrintL a + mymath()" you are recursively calling your function without an exit strategy. Are you sure about your code?
[@]to slow down your application, enter something like SLEEP [milliseconds] where [milliseconds] is the time the script has to stop


Eros

christianssen
09-03-2010, 14:29
thanks eros :)

a) does it mean I cannot calculate "PI" with different values ? (sorry)
(for example: pi*180/r)

b) how would this new example looks like with increase "mymath() function" and stop it at number I = 50 for exit ? I cannot manage. (sorry again) It's possible to increase a function? I don't think so. Was just an idea.

c) printl format$(c-a): good! I have saved in my mind, thanks!


with "PrintL a + mymath()" you are recursively calling your function without an exit strategy. Are you sure about your code?


no, I am no more sure about that ;)



' Empty GUI script created on 03-09-2010 10:22:46 by denis (ThinAIR)

Uses "console", "math"

MsgBox 0, "my multiply result: " + mymath()

Function mymath() As Long
Dim a,b,c As Long
Dim pival As Integer
Dim counter As Long

a = 5
b = 6
c = a*b

PrintL Format$(c + b)


PrintL Sin(a)
MsgBox 0," this is cos(a) result: " + Cos(a),%MB_OK, "pi " + Pi

PiVal = 4 * Atn(1)
PrintL "my pival result: " + pival

PrintL Log(b)
PrintL Format$(c-a)
counter += 1

Sleep 1000
Incr counter

PrintL a + counter '+ mymath() --- works, but fast list cannot Stop To see
If counter = 30 Then Exit Function

PrintL counter

Function = c

End Function

WaitKey



bye, thanks, denis

ErosOlmi
09-03-2010, 15:53
yes you can pi*180/r but not Pi(b) like in your first example. What is Pi(b)?
If you have a recursive function you must have an exit test otherwise you will enter into a loop and after a while a possible GPF or overflow. So what is your exit test here?
If your exit test is "If counter = 30 Then Exit Function"
you will never get that line of code because recursion is the line before it.

Solution?
Change
DIM counter As Long
to
Global counter As Long

and invert line
PrintL a + mymath()
with line
If counter = 30 Then Exit Function

Also note you are increasing Counter twice



Eros

D.J.Peters
09-03-2010, 16:02
hello chris, (christianssen)
what are you trying ?
i don't understand whats going on in your code :lol:

Joshy

' Empty ThinBASIC CONSOLE file template

Uses "Console"

Uses "console", "math"

MsgBox 0, "my multiply result: " + mymath()

Function mymath() As Long
Dim a,b,c,r As Long
Dim pival As Long
Dim counter As Long

a = 5
b = 6
c = a*b
r = c+b
PrintL "c+b = " + r


PrintL "sin(a) = " + Sin(a)
r= Cos(a) + Pi
PrintL " this is cos(a)+pi = " + r

PiVal = 4 * Atn(1)
PrintL "my pival result: " + pival

PrintL Log(b)
r=c-a
PrintL " c-a" = r
counter += 1

Sleep 1000
Incr counter
r=a+ counter + mymath()
PrintL "a + counter + mymath() =" r
If counter = 30 Then Exit Function

PrintL "counter=" + counter

Function = c

End Function

WaitKey

Lionheart008
09-03-2010, 17:40
hi denis, hi joshy, hi eros.

@denis:

if you use your first example with some function like "printl a + math()" you will get no exit for your function, neither for "counter = 30" nor "counter = 5". I have tested such similar things with functions and value increases some weeks before.
but with a loop (this I would prefer to do) you can have a serious exit if you want. don't know what's your intention? to combine a mathematical function with a loop, increasing values for what ? ;) I don't know that. I can suggest this kind of tb example:



' Empty GUI script created on 03-09-2010 10:22:46 by denis (ThinAIR)
' franks modification with loop and exit

Uses "console", "math"

Global counter As Long

PrintL

MsgBox 0, "my multiply result: " + mymath()

Function mymath() As Long
Dim a,b,c,d,i As Long
Dim pival As Integer
Dim counter As Long

a = 5
b = 6
c = a*b
d = Sin(a)

PrintL Format$(c + b)

PrintL "sin(a) is: " + d
Sleep 1000

MsgBox 0," this is cos(a) result: " + Cos(a),%MB_OK, "pi " + Pi

PiVal = 4 * Atn(1)
PrintL "my pival result: " + pival

PrintL Log(b)
PrintL Format$(c-a)
counter = 0
Do
Incr counter

Sleep 500
Console_WriteLine ("-- my counter :"+Str$(counter))
'PrintL

Function = c
Loop Until counter = 10 '----- Then Exit Do

PrintL

Sleep 100
'PrintL "mymath result: " + mymath()

End Function
PrintL
PrintL "again: mymath result: " + mymath()
PrintL
PrintL "pi(b) is sometimes one piece biscuit (b) cut off the pastry ?"
PrintL

WaitKey



2) if you activate "PrintL "mymath result: " + mymath() " before end function you will have the same dilemma that this function will never end and exit ;) you can test it.

eros has mentioned it already above:

If you have a recursive function you must have an exit test otherwise you will enter into a loop and after a while a possible GPF or overflow. So what is your exit test here?

thanks for your little example, frank

christianssen
10-03-2010, 11:12
thanks joshy, eros, frank for help.

think about best example for my idea was today with loop exit. because I am new with console modus I didn't see I cannot break the "PrintL "a + counter + mymath() " function for exit. that's new for me. sorry for unserious question or I didn't explain it clear. thanks to all for help! learning day by day one little cake of thinbasic :D

denis