Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upcoming changes to CONST may affect your code
#1
For folks that haven't bothered to read the various other topics on the forums here (especially one multiple pages deep), CONST is currently bugged.  At the moment, it's suffering from a glitch that was made obvious via the latest set of fixes to CONST.   (Fix one thing, break two more -- isn't that one of the core rules of programming?  LOL!)

At the moment, there are instances where CONST will *negate* the resulting value for us.   Const foo = 1 will assign a value of -1 to foo...   This is a 100% glitch and is being patched even now by the dev team. (It's not something so obvious as to work with 1, but the previous was just an illustration. Where you can find the glitch is with CONST foo = &HFFFF which returns a value of - &HFFF.)

BUT, while working on this patch, we also noticed that there was a secondary issue with CONST which needed fixing.  In the past, CONST has always returned UNSIGNED values to us with &H, &O, &B numbers.   This is 100% on me, as I'd just always considered hex values and such to be unsigned.  

THIS IS WRONG!!

Our guiding goal has always been QB45 compatibility, and I'd love for someone to show me where QB45 had an unsigned variable type...  Big Grin

In QB45, &HFFFF returns a value of -1, as it represents a SIGNED INTEGER value in hex.
In QB45, &HFFFFFFFF also returns a value of -1, as it represents an UNSIGNED LONG value in hex.

Due to Steve not realizing this, QB64 has been returning SIGNED values for those numbers in the past.   &HFFFF would return 65635 and &HFFFFFFFF would return 4billion-something.

Where this makes a drastic change would be if someone ever did something such as:

CONST foo = &HFFFF + &HFFFFFFFF

In QB45, the answer to that would be -- as shocking as it seems to my poor brain -- MINUS TWO.  
&HFFFF = -1 (as signed integer)
&HFFFFFFFF = -1 (as signed long)
-1 + -1 = -2 (add those together and get -2)

^That's just mind boggling to me personally, but it is what it is.  For our goal of maintaining backward's compatibility, CONST needs to have this same behavior.  Used outside of CONST, QB64 already does this type of bizarre type-casting.  It's just that I never accounted for this behavior in CONST itself, and now that oversight is going to be fixed soon.

And this fix may affect your code. 

If you relied on the same incorrect understanding that I did -- that unsuffixed values default to unsigned values, then you're going to be bit by these changes.

CONST foo = &HFF000000 + &HFFFF is no longer going to add up to be &HFF00FFFF. Instead, it's going to end up being &HFEFFFFFF. (It'll subtract that signed one instead of adding the unsigned 65535 for that integer.)

The fix here is rather simple: You need to manually assign the correct type to those hex values.

CONST foo = &HFF000000~& + &HFFFF~& <-- This makes both values unsigned longs, rather than default QB45 types of signed long and signed integer.

Be aware of this upcoming change and look over any existing code which you may be using. This might affect how QB64PE compiles and runs for you. We've had glitched behavior here with CONST giving signed values for &H, &O, and &B values, and that's being fixed. If your code *worked* under that glitch, it may *not* work now that the glitch is being fixed.

One particular place folks might want to double check their code is with color values.

CONST Blue = &HFF0000FF used to return unsigned long values to us. In the near future, once the patch is approved and added into the language, &HFF0000FF will instead give us a SIGNED long value.

As I mentioned above, the fix for this is to make certain you tell QB64 that you wanted it to be unsigned to begin with:

CONST Blue = &HFF0000FF~&

Unless you specify the type yourself, QB64 is going to try and follow the same logic that QB45 used in determining what variable type to return a value back to you. In the past, it failed at that determination and always returned unsigned values. That's being fixed, and if you relied on that glitch, then you'll need to go back and correct your code to keep up with the fix.
Reply
#2
Would the following line of code be affected?

CONST White = _RGB32(255, 255, 255, 255) ' FFFFFFFF
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#3
I wouldn't think so.  This only affects the &H, &B, &O calculations.

I'll double check to make certain though.  Wink
Reply
#4
(12-29-2023, 04:19 PM)SMcNeill Wrote: I wouldn't think so.  This only affects the &H, &B, &O calculations.

I'll double check to make certain though.  Wink
Ah, gotcha.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#5
[Image: image.png]


Upcoming changes would basically look like the above.

Code: (Select All)
CONST Blue = &HFF0000FF
CONST Blue2 = _RGB32(0, 0, 255)
CONST Blue3 = &HFF0000FF~&

PRINT Blue
PRINT Blue2
PRINT Blue3

Which results in: 

Quote:-16776961
4278190335
4278190335

(I just double checked for you.  As you can see, _RGB isn't affected by the change.)
Reply
#6
So, I don't really understand it anymore.  Huh

Code: (Select All)

'Const-Problem. https://qb64phoenix.com/forum/showthread...0#pid22330
'29. Dez. 2023

Const foo = 1
Print foo

Const foo1 = &HFFFF
Print foo1

Const foo2 = &HFFFF + &HFFFFFFFF
Print foo2

'Addition von Pluswerten, sonst Minuswert
Const foo3 = &HFF000000~& + &HFFFF~&
Print foo3

'4278255615 + (-65535) = 4.278.190.080
Const foo4 = &HFF000000~& + &HFFFF
Print foo4

Print
'Minuswert
Const foo5 = &HFF000000 + &HFFFF
Print foo5
Print
Print &HFF00FFFF
Print &HFEFFFFFF

[Image: Const-Problem2023-12-29.jpg]
Reply
#7
(12-29-2023, 05:33 PM)Kernelpanic Wrote: So, I don't really understand it anymore.  Huh

Code: (Select All)

'Const-Problem. https://qb64phoenix.com/forum/showthread...0#pid22330
'29. Dez. 2023

Const foo = 1
Print foo

Const foo1 = &HFFFF
Print foo1

Const foo2 = &HFFFF + &HFFFFFFFF
Print foo2

'Addition von Pluswerten, sonst Minuswert
Const foo3 = &HFF000000~& + &HFFFF~&
Print foo3

'4278255615 + (-65535) = 4.278.190.080
Const foo4 = &HFF000000~& + &HFFFF
Print foo4

Print
'Minuswert
Const foo5 = &HFF000000 + &HFFFF
Print foo5
Print
Print &HFF00FFFF
Print &HFEFFFFFF

[Image: Const-Problem2023-12-29.jpg]

You're still using the currently bugged version.

Quote:At the moment, there are instances where CONST will *negate* the resulting value for us. Const foo = 1 will assign a value of -1 to foo... This is a 100% glitch and is being patched even now by the dev team. (It's not something so obvious as to work with 1, but the previous was just an illustration. Where you can find the glitch is with CONST foo = &HFFFF which returns a value of - &HFFF.)


You're not going to see the changes yet, as they're still in the update queue and not live in the language yet. I was just giving folks early warning of *upcoming changes*, in the next release.

Wink
Reply
#8
Ok, that explains it. Thanks!
Reply
#9
Will this trigger a major version change (4.0)?
Reply
#10
(12-29-2023, 07:55 PM)dbox Wrote: Will this trigger a major version change (4.0)?

I dunno yet.  I guess that'll depend if the other devs think it's a widespread enough glitch to be labeled "code-breaking" and not just "bug-fixing".  Honestly, I don't think many folks will be affected by the change.  How many people are adding &HFFFF + &HFFFFFFFF without already using suffixes?

It has the *potential* to be code breaking, thus the early mention to see who all it might impact, but I really don't think we'll see a lot of people needing to change things to accommodate it.

In hex, the values don't really change.   &HFF000000 OR &HFF will still result in the same &HFF0000FF.  It's just those values are now signed instead of unsigned and that could affect things unintentionally, so-- at the least -- a sturdy warning is warranted, in my opinion, even if we don't get a full version bump from it.  Wink
Reply




Users browsing this thread: 3 Guest(s)