BASIC regret – no early exiting AND/OR
Pages: 1 2
nemo (145) 2546 posts |
I’ve just debugged an unexpected data abort in a very old bit of code. Long story made short:
All addresses correct. Why the abort? Because, for the first time in 19 years, the heap block in question happened to be exactly at the end of a Heap, which finished at the very end of the WimpSlot, with 0 bytes free. So, it reaches the end of the structure (E%) and knows it has finished… but thanks to AND it goes ahead and reads the next word anyway, which ain’t there. Sigh. Oh for:
|
GavinWraith (26) 1563 posts |
Why not use RiscLua?
|
Steve Drain (222) 1620 posts |
This problem of “lazy evalution” has cropped up a handful of times here. A search will find them. Apart from offering some work-arounds in BASIC, I wrote keywords into Basalt: LOGAND(<cond>[,<cond>]^]
is ‘logical AND’ and returns FALSE on the first failure, without evaluating the other conditions. LOGOR(<cond>[,<cond>]^]
is ‘logical OR’ and returns TRUE on the first success, without evaluating the other conditions. ;-) |
RonM (387) 60 posts |
WHILE Z%<E% && !Z%<O% The way I remember the discussions is that the && can be done with IF THEN and you can drop the THEN for brevity. Maybe brackets would be required for the NOT, I haven’t tried.
|
Steffen Huber (91) 1953 posts |
That’s why proper languages have both short-circuit AND full evaluation of logical operators. |
nemo (145) 2546 posts |
Does that mean if BASIC had short-circuited binops it would be a proper language? Just kidding! Ron suggested
Yeah, that’s the way to short-circuit IFs, but WHILEs are a bit more awkward. I fixed it by making the block 4 bytes longer than it needed to be. Makes me wonder how many other times I’ve written that compromised construction though. :-/ Steve humbled
Typical! |
Steve Drain (222) 1620 posts |
They are, but would this work? WHILE Z%<E% IF !Z%<O% THEN <do it> ELSE Z%=E% ENDIF ENDWHILE |
RonM (387) 60 posts |
but WHILEs are a bit more awkward. A while loop can usually be replaced with the faster REPEAT loop but it might |
Ron (2686) 63 posts |
I guess || is the NOT version of && Wouldn’t that be for bitwise ‘OR’ rather than ’NOT ? Ron |
RonM (387) 60 posts |
Wouldn’t that be for bitwise ‘OR’ rather than ’NOT ? The way I was looking at it the logical || will drop through as soon as a condition is met, the inverse to && which drops through when it is (NOT) met. Bitwise is a lower level construct perhaps? Probably bitwise can do everything logical can do, logical is a higher level convenience. Just the way I’m seeing it, there are likely better explanations. |
Steve Drain (222) 1620 posts |
Why do you say that? A brief test suggests the differences are very small and that REPEAT is faster for a small number of iterations, but slower for a large number. The swap over is in the teens. That could be accounted for by the larger overhead in setting up a WHILE loop, but the more efficient evaluation of the test condition. In loops with substantial content the differences will be insignificant. |
Steve Drain (222) 1620 posts |
You really require: WHILE Z%<E% IF !Z%<O% THEN EXIT <do it> ENDWHILE Guess what? ;-) |
Steffen Huber (91) 1953 posts |
Not sure of the correct english words (always did Math in German only), but I think it is “necessity” and “sufficiency” that needs to be distinguished here :-) |
Clive Semmens (2335) 3276 posts |
Your English is spot on, Steffen. I just wish my German was good enough to read your blog properly. |
Steffen Huber (91) 1953 posts |
I was recently pleasantly surprised by the quality of Google Translate wrt my blog posts (in contrast to many other German text sources!) – I know several English people who use it regularly to read my blog. |
Clive Semmens (2335) 3276 posts |
Ah – interesting. I’ll give it a whirl. I was interested in your bit about your 4K monitor – I have one, and I’ve got lots of modes working on it, but all at 24 Hz. Can the 3B+ manage faster than that at 4K? Not that I mind 24Hz, that’s quite fast enough for me. |
Steve Pampling (1551) 8170 posts |
|
Clive Semmens (2335) 3276 posts |
That bit is pretty reliable. The sad thing is that “Gold in, Garbage out” is also perfectly possible and not that unusual. Vietnamese → English was still pretty rubbish last time I needed it, and I don’t think my friend was feeding me garbage. |
Steve Pampling (1551) 8170 posts |
I refer the esteemed gentleman to consider point 1 with a lack of decent teaching, so basically point 1 and 2 together. |
Rick Murray (539) 13840 posts |
My only wish/regret with Google Translate is that it does not mark unknown words with square brackets. And maybe entire unknown phrases likewise? Using colloquial expressions can arrive at some very peculiar results: English: The dozy b*nt fell asleep. French: Le b*nt doudou s’est endormi Clearly the impolite word is unknown and it seems to think “dozy” is a blanket, like a child’s security blanket. Let’s try that again, shall we? French: La peluche fumeuse s’endormit. You what? I don’t even want to think what logical process arrived at that conclusion. Also, while I’m thinking of it: Let’s see if I can get my leg over this weekend. Result: Voyons voir si je peux obtenir ma jambe ce week-end. Which is a very literal blind idiot translation that has discarded the word “over” to arrive at a bizarre sentence where I want to go get my leg this weekend, because, you know, it fell off and got left in the car park… |
Steffen Huber (91) 1953 posts |
I have not tried more than 24Hz with 4K yet. I will first have to check all the older Pis for 4K capability. 30Hz should be possible, 60Hz won’t be possible because of the pixel clock limit and memory bandwidth/scaler issues and possibly the HDMI transmitter will also not allow for that bandwidth. At least that’s what I read in the Pi forum. Some people reported that they had to use force_turbo to get 4K at all (even at 24Hz, but reports were for Pi 2 IIRC). Not sure if it is a CPU, GPU or memory issue – I would guess memory. |
RonM (387) 60 posts |
I was recently pleasantly surprised by the quality of Google Translate Steffen, Could you look at my GT attempt at warnung in german here A brief test suggests the differences are very small and that REPEATThe test I did once (many iterations of the loop) did show a reasonable diifference, but as you say in practice more importance should be given to tidy easy to read BASIC for the next person or yourself down the track. A little late now, but I just thought of names for && and || could have been NDT and DT which describes there actions rather than names with similarity to Bitwise or math. For me the relationship with the CLI/Obey ‘If Then Else’ and the usage of is of more importance. Edit: I have seen the term ‘advance’ used for progressing across a line. perhaps Advance if True and Advance if False instead of the unclear NOT
|
Clive Semmens (2335) 3276 posts |
Works OK on my Pi B rev 2, also on my Pi 3B – at 24Hz. Doesn’t seem to manage 25Hz, never mind 30Hz. The Pi thinks it’s doing 25Hz, but the monitor says 24Hz, although it has 25Hz and 30Hz (which the Mac Mini drives okay). |
nemo (145) 2546 posts |
Steve asked
Nope. Finding a place in an ordered list of packed variable sized nodes, so two simultaneous exit conditions, the second predicated on the success of the first. The thinko (not typo) was not in the nature of AND, but of the possible memory map. Fixed now. Sheer (astronomical) bad luck that it would be adding a new maximum to a list filling its allocation when the allocation is at the end of a heap with 0 bytes free. /me buys lottery ticket. RonM claimed
Nope. WHILE and REPEAT are at least ten times slower than FOR, but if you wanted a fight with a significant number of repetitions, WHILE is slightly faster. Steve returned with
Stage left. |
Steve Drain (222) 1620 posts |
Just to muddy the waters, would you be able to use: DO [WHILE <cond>] ... LOOP [UNTIL <cond>] with |
Pages: 1 2