Author Topic: Association of numbers  (Read 4640 times)

0 Members and 1 Guest are viewing this topic.

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Association of numbers
« on: November 04, 2020, 06:09:58 pm »
Hi Blus, I was nervous, reading the scoldings in mynameispaul's post and realized afterwards that you understood what I was trying to do.

Here is an image of another solution that you solved for me.

I learned that I don't have to worry, and next time, I will be more careful in this wonderful forum.

Thank you very much Bplus once again.
Carlos,
Associação.jpg
* Associação.jpg (Filesize: 226.61 KB, Dimensions: 1029x983, Views: 311)

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Association of numbers
« Reply #1 on: November 04, 2020, 06:26:48 pm »
Bplus, I have two more questions

always like to put the zeros in the counters
The only way I found was this:

FOR i = 1 TO 1000
    LOCATE 2, 2: PRINT RIGHT$("0000", (LEN("0000") - LEN(LTRIM$(STR$(i))))) + LTRIM$(STR$(i))
    _DELAY .1
NEXT
How would you do it your way?
I mean, is there any way to shorten the line otherwise?
counter.jpg
* counter.jpg (Filesize: 75.49 KB, Dimensions: 1021x580, Views: 257)
« Last Edit: November 04, 2020, 06:28:12 pm by carloscordeiro »

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Association of numbers
« Reply #2 on: November 04, 2020, 06:32:39 pm »
The other question, is, is there in qbasic64 any instructions like input or INPUT $, to paste a ready sentence "The famous ctrl C and ctrl V" at the levels of typing it, or qbasic accepts only by typing.

Carlos

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Association of numbers
« Reply #3 on: November 04, 2020, 07:16:16 pm »
Quote
...
I learned that I don't have to worry, and next time, I will be more careful in this wonderful forum.

Thank you very much Bplus once again.
Carlos,

Good and I guess I did get it with the help of the 2nd reply.

2nd post

Here is a couple of other ideas:
Code: QB64: [Select]
  1. FOR i = 1 TO 1000
  2.     LOCATE 2, 2: PRINT RIGHT$("0000" + LTRIM$(STR$(i)), 4) ' a little shorter
  3.     '_DELAY .1 ' this is fine!  1/10 sec
  4.     _LIMIT 17 ' 10 / 60 sec  another way?
  5.  

3rd post: paste in an input?
Mr @SMcNeill did favor us with just that option one time. I will see if I can did it up. It's just a mix of INKEY$ or _KEYHIT and _CLIPBOARD$.
« Last Edit: November 04, 2020, 07:17:25 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Association of numbers
« Reply #4 on: November 04, 2020, 07:26:34 pm »
Good and I guess I did get it with the help of the 2nd reply.

2nd post

Here is a couple of other ideas:
Code: QB64: [Select]
  1. FOR i = 1 TO 1000
  2.     LOCATE 2, 2: PRINT RIGHT$("0000" + LTRIM$(STR$(i)), 4) ' a little shorter
  3.     '_DELAY .1 ' this is fine!  1/10 sec
  4.     _LIMIT 17 ' 10 / 60 sec  another way?
  5.  

3rd post: paste in an input?
Mr @SMcNeill did favor us with just that option one time. I will see if I can did it up. It's just a mix of INKEY$ or _KEYHIT and _CLIPBOARD$.

This one?  https://www.qb64.org/forum/index.php?topic=1954.msg111798#msg111798
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Association of numbers
« Reply #5 on: November 04, 2020, 07:36:34 pm »
OK Bplus
I'll use your idea, much shorter. Great!!!
FOR i = 1 TO 1000
    LOCATE 2, 2: PRINT RIGHT$("0000" + LTRIM$(STR$(i)), 4) ' a little shorter
    '_DELAY .1 ' this is fine!  1/10 sec
    _LIMIT 17 ' 10 / 60 sec  another way?
NEXT

_LIMIT  17 is Excellent!

Thanks


Thanks also to Mr @SMcNeill

Carlos
Cout.jpg
* Cout.jpg (Filesize: 136.8 KB, Dimensions: 977x1063, Views: 259)
« Last Edit: November 04, 2020, 07:41:19 pm by carloscordeiro »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Association of numbers
« Reply #6 on: November 04, 2020, 08:22:32 pm »
Hmm... math is giving me trouble tonight it might be
_LIMIT 10  as the equivalent to _DELAY .1

_LIMIT is in units of frames or loop counts per second, 10 frames in a second is 1/10 sec per frame.

Anyway there is an advantage to _LIMIT, it frees up the CPU whereas _DELAY is not advertised as an CPU advantage:
 
_DELAY versus _LIMIT.PNG


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Association of numbers
« Reply #7 on: November 04, 2020, 08:35:39 pm »
Delay and limit will both free up CPU usage.

The main difference is delay is hard coded; limit is a variable amount.

For example, I have a program that does some extensive math calculations in a loop that takes .05 seconds to process.

DO
   MathJunk
   _DELAY .1
LOOP

Now, with the above, I process about 6 loops per second.  The math takes .05 seconds, the delay takes .1, so .15 seconds per loop...  or a little over 6 times per second.

Now, we use _LIMIT:

DO
   MathJunk
   _LIMIT 10
LOOP

Here, QB64 runs a timer and calculates the delay for your PC automatically.  If the math takes .05 seconds, the delay becomes .05 seconds.  If the math takes .1 second, the delay becomes 0.0 seconds. 

Limit sets the maximum number of times you repeat a loop in a second; Delay is a hard-coded pause in execution.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Association of numbers
« Reply #8 on: November 05, 2020, 10:09:39 am »
I was thinking of asking exactly the difference between _DELAY and _LIMIT.

Thank you very much for an answer before I ask.

 Carlos.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Association of numbers
« Reply #9 on: January 31, 2021, 10:28:00 pm »
bplus
Quote
Anyway there is an advantage to _LIMIT, it frees up the CPU whereas _DELAY is not advertised as an CPU advantage:

That got me thinking. How does _LIMIT actually free up the CPU?  We know that LIMIT regulates the number of LOOPs per second.   Does _LIMIT use it's own LOOP to idle or does it use the OS to vary the time slice that the CPU gives the program that is running?



Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Association of numbers
« Reply #10 on: January 31, 2021, 11:19:05 pm »
I don't know what the hardware equivalent of this would be but I see Mr Limit standing at a gate with a timer and when the timer dings Mr Limit opens the gate at precisely that time.

Now Mr Execution comes along and if he is running fast he has to wait at the gate for Mr Limit to hear the ding and open the gate and if Mr Execution is running late he just flies right by because the gate is already open.

Now the question is when does the timer get reset, after Mr Execution passes or after Mr Limit opens the gate? I suspect the former though the event of the later could be the signal to reset, I don't know.

Come to think, if Mr Execution is running over twice as late as the timer at regular intervals I'd think the gate would still be open from the first timer ding. It's just logic that says the timer has to be reset when Mr Execution passes that point in code sequence.

So I see some sort of timer system that tells the OS just be back here at such and such a time so I can open the gate or maybe it's the OS that opens the gate.
« Last Edit: January 31, 2021, 11:59:29 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Association of numbers
« Reply #11 on: February 01, 2021, 12:20:38 am »
If you’re curious, here’s limit from our c-code:

Code: C++: [Select]
  1.  void sub__limit(double fps){
  2.     if (new_error) return;
  3.     static double prev=0;
  4.     double ms,now,elapsed;//cannot be static
  5.     if (fps<=0.0){error(5); return;}
  6.     ms=1000.0/fps;
  7.     if (ms>60000.0){error(5); return;}//max. 1 min delay between frames allowed to avoid accidental lock-up of program
  8.     recalculate:
  9.     now=GetTicks();
  10.     if (prev==0.0){//first call?
  11.         prev=now;
  12.         return;
  13.     }
  14.     if (now<prev){//value looped?
  15.         prev=now;
  16.         return;
  17.     }
  18.     elapsed=now-prev;//elapsed time since prev
  19.    
  20.     if (elapsed==ms){
  21.         prev=prev+ms;
  22.         return;
  23.     }
  24.    
  25.     if (elapsed<ms){
  26.         int64 wait;//cannot be static
  27.         wait=ms-elapsed;
  28.         if (!wait) wait=1;
  29.         if (wait>=10){
  30.             Sleep(9);
  31.             evnt(0);//check for new events
  32.             }else{
  33.             Sleep(wait);  
  34.         }
  35.         //recalculate time
  36.         goto recalculate;
  37.     }
  38.    
  39.     //too long since last call, adjust prev to current time
  40.     //minor overshoot up to 32ms is recovered, otherwise time is re-seeded
  41.     if (elapsed<=(ms+32.0)) prev=prev+ms; else prev=now;
  42. }

As you can see, the heart of it is a simple call to Sleep(milliseconds), and like bplus’s analogy — a simple stopwatch to set the number of milliseconds to sleep each cycle.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Association of numbers
« Reply #12 on: February 01, 2021, 12:28:45 am »
Oh the code resets the timer! every time it hits _LIMIT, simple.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Association of numbers
« Reply #13 on: February 01, 2021, 01:20:38 am »
Looked at the WIN32 stuff and there is a SLEEP API.    https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep