seba
New Member
Posts: 4
|
Post by seba on Dec 7, 2019 1:15:56 GMT -5
Hi, I am SUPER rusty with Liberty Basic and need help.
I have very basic programming skills to do things I can't get done with Excel. So right now I have an infinite While..Wend loop which does some work for me and it does the job, however I'd like to give the laptop a break from time to time by pausing the program somehow. I'm not sure if it is possible. I know I've done something like this years ago but I had like a popup window and I'd click the close "X" and it would popup "quit or cancel" and I'd just leave it like that which accomplished my goal in a very rudimentary way lol. Now I can't even do that anymore though
|
|
|
Post by Rod on Dec 7, 2019 9:16:46 GMT -5
Well this is just one way.
nomainwin WindowWidth = 400 WindowHeight = 400 UpperLeftX = int((DisplayWidth-WindowWidth)/2) UpperLeftY = int((DisplayHeight-WindowHeight)/2) button #1.bp, "Pause", [pause], UL, 100, 100, 200,20 button #1.br, "Resume", [resume], UL, 100, 130,200, 20 textbox #1.tb 100,160,200,20 open "Pogram Manager" for window as #1 print #1, "trapclose [quit]" processing=1
[loop] scan if processing = 0 then wait count=count+1 #1.tb count 'your code here goto [loop]
[pause] processing=0 goto [loop]
[resume] processing=1 goto [loop]
|
|
|
Post by Carl Gundel on Dec 7, 2019 12:43:16 GMT -5
Hi, I am SUPER rusty with Liberty Basic and need help. I have very basic programming skills to do things I can't get done with Excel. So right now I have an infinite While..Wend loop which does some work for me and it does the job, however I'd like to give the laptop a break from time to time by pausing the program somehow. I'm not sure if it is possible. I know I've done something like this years ago but I had like a popup window and I'd click the close "X" and it would popup "quit or cancel" and I'd just leave it like that which accomplished my goal in a very rudimentary way lol. Now I can't even do that anymore though
You should probably use a TIMER. It will greatly reduce the CPU time used when idling. while 1 = 1
'the timer will make it stop for 1000 milliseconds and then on to [proceed] timer 1000, [proceed] wait
[proceed] timer 0 'clear the timer after we proceed
cls print "Run my periodic routine! "; time$() print left$(httpget$("http://www.libertybasic.com"), 1234)
wend
When you run this example and check the task manager for CPU consumption of liberty.exe you will see that it is very low.
|
|
seba
New Member
Posts: 4
|
Post by seba on Dec 28, 2019 5:50:01 GMT -5
This works perfectly thank you!!
|
|
seba
New Member
Posts: 4
|
Post by seba on Dec 28, 2019 6:33:01 GMT -5
I have however a new problem, if I can call it new, I've probably been having this problem for some time without even realizing it. I don't know if this is a problem with my Laptop CPU or if everyone has the same result but as an example if I write:
PRINT (0.2-0.6) - (0-0.4)
This should show: 0
instead I get: 0.55511151e-16
Strangely
PRINT VAL(STR$(0.2-0.6)) - VAL(STR$(0-0.4))
gives me: 0
So now I have to go back and figure out where I have to make this decimal to STR back to VAL conversion in my program, is there a better way to do this? My numbers are often not just a single decimal space so I don't think I can just multiply up and cut the error off with INT()
|
|
|
Post by Rod on Dec 28, 2019 9:20:26 GMT -5
Summing floats NEVER ends up at zero, nearly zero, but NEVER exactly zero. No big deal you just need to understand that when using floats you will have a tiny trailing value that will need to be considered. Things like float = float or float + float = 0 are very unlikely to be true. Its the way floating point maths works on all computers.
The other thing about floats is that you are rarely shown the entire float it is often truncated for display. You can use that technique yourself using the USING() command.
PRINT using("#.##",(0.2-0.6) - (0-0.4)) 'show same number at 15 decimal places
PRINT using("#.###############",(0.2-0.6) - (0-0.4))
'show same number at 16 decimal places
PRINT using("#.################",(0.2-0.6) - (0-0.4))
google for a float tutorial, or just like me, accept that they are a bit wooly round the edges and you only need to worry about it when you compare two floats. Adding, subtracting, multiplication and division are not a concern and preserve precision.
|
|
|
Post by tsh73 on Dec 28, 2019 11:44:23 GMT -5
(I think )There is no way to avoid it. in pretty much any language that uses CPU number formats (C Python anything) Just like 1/3 cannot be expressed in decimals exactly (you can write 0.33333... but you have to stop eventually), same way is many ordinary numbers are could not be expressed in binary. Like, 1/10 which is 0.1 in decimal which is in binary (help me Google) decimal-to-binary.com/decimal-to-binary-converter-online.html?id=9440.00011001100(forever 1100, but you have to stop somewhere, and Double just 32 bit long) So you just have to keep that in mind and compare real numbers with accuracy set like eps = 1e-10
y = .1+.1+.1+.1+.1+.1+.1+.1+.1+.1 '10 times .1 z = 1
print y, z
print y-z
if y = z then print "equal" else print "not equal"
if abs(y - z)<eps then print "approx.equal" else print "approx. not equal"
1.0 1 -0.1110223e-15 not equal approx.equal
|
|
seba
New Member
Posts: 4
|
Post by seba on Dec 28, 2019 15:49:32 GMT -5
Ah ok, yes I found it when I wrote in something to compare 2 values, it took me 2 days to figure out it wasn't just another stupid mistake I made. Thank you!
I ended up using the approximate 0 variable trick and all my errors are gone.. until I do more work to the program at least lol
|
|
|
Post by bencooper on Jan 1, 2020 15:26:30 GMT -5
Whenever I wish to compare a computed number (x) with a given number (y) (as in many newspaper Teaser problems) I will use IF ABS(x-y)<1E-8 (say) to avoid that pesky rounding error getting in the way!
|
|