Ubyte and negative step in 'For' loop [Solved]

New to FreeBASIC? Post your questions here.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Ubyte and negative step in 'For' loop [Solved]

Post by Tourist Trap »

Please, do anyone know what the reason for this :

Code: Select all

For _index As UByte = 2 To 0 Step -1
	? _index
	If _index > 2 Then 
		? "-(?)-"
		Exit For 
	EndIf
Next _index

Sleep 
Anyway, if changing ubyte by byte, everything runs ok and loop stops at 0.
I perfectly understand that -1 = 255, for unsigned byte, but this value shouldn't be reached here.
UByte Standard data type: 8 bit unsigned

Syntax
Dim variable As UByte

Description
8-bit unsigned whole-number data type. Can hold a value in the range of 0 to 255
Last edited by Tourist Trap on Jul 25, 2015 20:59, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Ubyte not compatible with negative step in For loop?

Post by MrSwiss »

Tourist Trap wrote:Please, do anyone know what the reason for this
Short answer: logic says its wrong, to use an unsigned Type variable to compare with a negative (Step) value (signed).

In more detail:
I suppose the compiler makes a TypeOf() check, to see whether all parameters are of a compatible Type.
This would then return "incompatible" because the Type needed to hold a negative value must be a signed Type,
the size in Bytes (1, 2, 4, 8) of the Type is irrelevant in this case.

AFAIK in earlier Versions of FBC this sort of assignment didn't trigger an "exception", but this behavior was clearly wrong.
There is IMO no use case for such misuse of variables ...
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Ubyte not compatible with negative step in For loop?

Post by Tourist Trap »

MrSwiss wrote:
Tourist Trap wrote:Please, do anyone know what the reason for this
Short answer: logic says its wrong, to use an unsigned Type variable to compare with a negative (Step) value (signed).
Sorry but in this for loop, when 0 is reached there is no need to compare with any negative value --> end of loop. There is no problem with Step-1, unless your explanation means that an unsigned can not do a substraction.
Thank anyway.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Ubyte not compatible with negative step in For loop?

Post by MrSwiss »

The incrementor/decrementor variable in the loop has to be the same Type as the Step variable!

Code: Select all

' Single/Single
For i As Single = 1 To 2 Step 0.001
    ' ... code here
Next
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Ubyte not compatible with negative step in For loop?

Post by Tourist Trap »

Mr. Swiss, what does it change? The arithmetic works perfectly, either you substract -1 or you add 255. Have you tried to launch the example I've posted? You would see that the only problem is when 0 is reached for the index (and it is) then the loop doen't stop. Otherwise it does what it should, really.

Code: Select all

2
1
0     
255

If the step wasn't working it would not even reach 0. Since this bound is reached why the for loop misses the detection over the index variable, that's what I cant figure out.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Ubyte not compatible with negative step in For loop?

Post by fxm »

I think that at each loop, the comparision is executed after the decrementation.
With an Ubyte, the test '_index < 0' will be never verified.
Last edited by fxm on Jul 25, 2015 20:25, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Ubyte not compatible with negative step in For loop?

Post by MrSwiss »

Tourist Trap wrote:why the for loop misses the detection over the index variable, that's what I cant figure out.
Simply because the next expected value would be -1 (which an UByte can't represent).
You are getting 255 which, on a UByte, is correct, but:
it is of course > 0 (you'll never get the condition < 0 to evaluate as TRUE).
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Ubyte not compatible with negative step in For loop?

Post by Tourist Trap »

fxm wrote:I think that at each loop, the comparision is executed after the decrementation.
With an Ubyte, the test 'index < 0' will be never verified.
Possible yes. But at least for integer types including the limit would be more useful. If index <= 0 then exit.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Ubyte not compatible with negative step in For loop?

Post by fxm »

The proof:

Code: Select all

Dim As UByte _index
For _index = 3 To 1 Step -1
  Print _index
Next _index
Print
Print _index

Sleep

Code: Select all

3
2
1

0
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Ubyte not compatible with negative step in For loop?

Post by jevans4949 »

This comes up quite often.

The way a for-loop works is that at the end of the loop the counter is incremented (or in this case decremented) in accordance with the STEP clause, then tested to see if it has surpassed the TO value, in the relevant direction. FB does not test or report for overflow/underflow of the counter when STEP is applied (for speed), so in your case, if a ubyte counter is at 0, the STEP operation changes it effectivey to 255, which is not less than 0, so the loop continues indefinitely. If you want to STEP wih a negative value TO zero, use a signed integer type. If you want to STEP positively TO 255 use a (signed or unsigned) SHORT (or bigger). Similarly for all maximum / minimum values for integer types. The net result is an infinite loop repeatedly through all values - in your case 2,1,0,255 ... 0, 255 ... 0 ...

There is no reason why you cannot subtract from an unsigned integer type, as long as you are aware that going below zero will give you an unexpected problem.

Sorry, but this is a well-understood feature of FOR-loops in most compiled languages. You will just have to get with the program.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Ubyte not compatible with negative step in For loop?

Post by MrSwiss »

Tourist Trap wrote:
fxm wrote:I think that at each loop, the comparision is executed after the decrementation.
With an Ubyte, the test 'index < 0' will be never verified.
Possible yes. But at least for integer types including the limit would be more useful. If index <= 0 then exit.
NEVER in a lifetime ...
This would in effect screw up the existing logic totaly!
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Ubyte not compatible with negative step in For loop?

Post by Tourist Trap »

jevans4949 wrote:Sorry, but this is a well-understood feature of FOR-loops in most compiled languages. You will just have to get with the program.
Thanks, and don't be sorry, it's the first time I'm facing this so I've been surprised. I'll try to avoid 'for...next' with short range types. The truth however is that For...next maybe should also be avoided in the case where the boundaries are variable in order to prevent never ending loops.

Code: Select all

For index = 1 to b    'who knows if b won't reach a limit in any maneer?
MrSwiss wrote:
Tourist Trap wrote: at least for integer types
This would in effect screw up the existing logic totaly!
It would only change the logic for integer types if you read me precisely.
fxm wrote:The proof:

Code: Select all

3
2
1

0
Damn. I would never have expected such a betrayal from my favourite loop.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Ubyte not compatible with negative step in For loop?

Post by fxm »

Tourist Trap wrote:The truth however is that For...next maybe should also be avoided in the case where the boundaries are variable in order to prevent never ending loops.
No problem with variable boundaries.
Their values are evaluated once only when entering the For Loop:

Code: Select all

Dim As Integer I0 = 0
For I As Integer = 10 To I0 Step -1
  Print I
  I0 += 1
Next I
Print
Print I0

Sleep

Code: Select all

 10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0

 11
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Ubyte and negative step in 'For' loop [Solved]

Post by MrSwiss »

Tourist Trap wrote:
at least for integer types
MrSwiss wrote:
This would in effect screw up the existing logic totaly!
Tourist Trap wrote:
It would only change the logic for integer types if you read me precisely.
You really seem to be slow ... there is only one logic for all Types!
Otherwise you'd slow the whole thing (FBC) down which is definitely a "NOGO".
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Ubyte and negative step in 'For' loop [Solved]

Post by jevans4949 »

By the way, there is a thing in the compiler to prevent the use of constants out of range for the data type. So:

For i As ubyte = -1 To 300

- will throw up:

warning 25(0): Overflow in constant conversion

Also, the following type of logic may be useful to you at some point in your career.

Code: Select all

Dim mytable(1 To 5) As String = {"a","b","c","d","e"}
Dim mystring As String
Dim myi As Integer
Input mystring
While mystring<>"z"
	For myi = LBound(mytable) To UBound(mytable)
		If mystring = mytable(myi) Then
			Exit For
		EndIf
	Next
	If myi > UBound(mytable) Then
		Print "Not Found"
	Else
		Print "Found at",myi
	EndIf
	Input mystring
Wend

Post Reply