It is currently Fri Aug 23, 2019 12:08 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 6:27 pm 
Offline
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1050
Location: Russia
In this code a-4=254. But they are calculated as signed variables. Why?
Code:
a.a = 2
b.a = a-4
Debug b
Debug Bool(a-4>0)
If a-4>0
  Debug "a>0"
EndIf

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 6:40 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3639
Location: Berlin, Germany
User_Russian wrote:
In this code a-4=254. But they are calculated as signed variables. Why?
Code:
a.a = 2
b.a = a-4
Debug b
Debug Bool(a-4>0)
If a-4>0
  Debug "a>0"
EndIf

No, b is calculated as unsigned variable, that's why Debug b shows 254 (PB 5.71 beta 3).
If b were calculated as signed variable, Debug b would show -2.

I can't see any bug there.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 7:53 pm 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1847
Location: Germany
This is normal binary calculation with over and underflow of values.
You have to take care to calculate in the right range.

No Bug 8)

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 8:34 pm 
Offline
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1050
Location: Russia
If the variable is unsigned (the variable a is unsigned), the calculations must be unsigned.
Why are signed calculations performed for an unsigned variable?

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 9:02 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3639
Location: Berlin, Germany
User_Russian wrote:
If the variable is unsigned (the variable a is unsigned), the calculations must be unsigned.

Who says so (except from you)?
I think the terms "signed" and "unsigned" apply when values are assigned to the respective variable, not when using it in calculations.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Tue Aug 13, 2019 9:26 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1211
Location: Germany, Glienicke
The comparison operators in PB are always signed, independent from the variable type.
You have to use ASM like this:
Code:
Procedure AboveByte(a.a, b.a)
   !MOV cl, [p.v_a]
   !MOV dl, [p.v_b]
   !CMP cl, dl
   !JNA @F
   ProcedureReturn 1
   !@@:
   ProcedureReturn 0
EndProcedure

a.a = 2
b.a = a-4
Debug b
Debug Bool(AboveByte(a-4,0))
If AboveByte(a-4,0)
  Debug "a>0"
EndIf

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Wed Aug 14, 2019 5:22 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3639
Location: Berlin, Germany
Or first assign the result of an expression to an unsigned variable, and then use that variable for comparisons, rather than the original expression itself:
Code:
a.a = 2
b.a = a-4
Debug b
Debug Bool(b > 0)
If b > 0
   Debug "b > 0"
EndIf


BTW: Wilbert posted code for doing unsigned comparisons:
viewtopic.php?p=419868#p419868

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Wed Aug 14, 2019 8:58 am 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1847
Location: Germany
Link Unsigned: viewtopic.php?f=12&t=73280
Code:
Macro Unsigned (_value_)
   (((1 << (8*SizeOf(_value_))) - 1) & _value_)
EndMacro

a.a = 2
b.a = a-4
Debug b
Debug Bool(b > 0)
If Unsigned(b) > 0
   Debug "b > 0"
EndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 12:01 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3386
Location: New Zealand
I'd call it a bug the Bool and If should do an unsigned comparison

Code:
;EnableUnsigned  v1.0a   
;Author Idle 25/3/16
;Supports PB 5.42LTS Window, Linux 
;Unsigned arithmetic and logical comparisons for   
;x64: Long, Integer, Quads
;x86: Long, Integer
;Usage:
;Scope unsigned arithmetic and logic in EnableUnsigned() DisableUnsigned() blocks
;EnableUnsigned() 
;if ux > uy  ; changes the operators to unsigned: supports < > <= >= <> =   
;    do something unsigned: supports * / + - << >> 
;  DisableUnsigned()
;   ;back to signed 
;EndIf
;Disableunsigned() ;Note you can call Disableunsigned() where ever you need it


CompilerIf SizeOf(integer) = 4
 
 
Macro EnableUnsigned()
 
  !macro IDIV var
  !{
     !mov edx,0
     !div var
  !}
 
  !macro IMUL reg,var
  !{
    !mov eax,reg
    !mul var
    !mov reg,eax
  !} 
 
  !macro SAR reg,var
  !{
    !shr reg,var
  !}
 
  !macro SAL reg,var
  !{
   !shl reg,var
  !}
   
  !macro CDQ {}
 
  !macro JG arg
  !{
  !JA arg
  !}
  !macro JGE arg
  !{
     !JAE arg
  !}
  !macro JL arg
  !{
     !JB arg
  !}
  !macro JLE arg
  !{
     !JBE arg
  !}
 
EndMacro

Macro DisableUnsigned()
  !purge IDIV
  !purge IMUL
  !purge SAR
  !purge SAL
  !purge CDQ
  !purge JG
  !purge JGE
  !purge JL
  !purge JLE
EndMacro

CompilerElse
   
  Macro EnableUnsigned()
 
  !macro IDIV var
  !{
     !mov rdx,0
     !div var
  !}
 
  !macro IMUL reg,var
  !{
  !match =qword x , var
  !\{ mov rax, reg
     !mov r15, var
   !\}
    !mul reg
    !mov reg,rax
  !} 
 
  !macro SAR reg,var
  !{
    !shr reg,var
  !}
 
  !macro SAL reg,var
  !{
   !shl reg,var
  !}
   
  !macro MOVSXD reg,var 
  !{
    !match =dword x , var
    !\{ mov eax, var
       !mov reg,rax \}
    !}
  !macro CQO {}
  !macro CDO {}
  !macro CWD {}
  !macro CBW {}
  !macro CWDE{}
  !macro CDQE {}
  !macro CDQ {}
 
  !macro JG arg
  !{
  !JA arg
  !}
  !macro JGE arg
  !{
     !JAE arg
  !}
  !macro JL arg
  !{
     !JB arg
  !}
  !macro JLE arg
  !{
     !JBE arg
  !}
 
EndMacro

Macro DisableUnsigned()
  !purge IDIV
  !purge IMUL
  !purge SAR
  !purge SAL
  !purge MOVSXD
  !purge CQO
  !purge CDO
  !purge CWD
  !purge CBW
  !purge CWDE
  !purge CDQE
  !purge CDQ
  !purge JG
  !purge JGE
  !purge JL
  !purge JLE
EndMacro   

CompilerEndIf   


a.a = 2
b.a = a-4   
Debug b

EnableUnsigned()
Debug Bool(a-4>0)
If a-4>0
  Debug "a>0"
EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 6:53 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3639
Location: Berlin, Germany
idle wrote:
I'd call it a bug the Bool and If should do an unsigned comparison

As far as I can see, doing always signed comparison is the intended behaviour (as Stargate already wrote). So it can hardly be called a bug.
Asking for unsigned comparison in particular cases is a feature request.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 7:24 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3568
Location: Utah, USA
Little John wrote:
idle wrote:
I'd call it a bug the Bool and If should do an unsigned comparison

As far as I can see, doing always signed comparison is the intended behaviour (as Stargate already wrote). So it can hardly be called a bug.
Asking for unsigned comparison in particular cases is a feature request.


In addition, IMHO the sample code is a worthless example:
Code:
a.a = 2
b.a = a-4
Debug Bool(a-4>0) ;How is Bool() supposed to know you want an unsigned comparison?
If a-4>0          ;How is IF supposed to know you want an unsigned comparison?


If the comparison 'a-4 > 0' is to be treated as unsigned it should be rewritten as 'a-4 <> 0'.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 10:45 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3386
Location: New Zealand
Demivec wrote:
Little John wrote:
idle wrote:
I'd call it a bug the Bool and If should do an unsigned comparison

As far as I can see, doing always signed comparison is the intended behaviour (as Stargate already wrote). So it can hardly be called a bug.
Asking for unsigned comparison in particular cases is a feature request.


In addition, IMHO the sample code is a worthless example:
Code:
a.a = 2
b.a = a-4
Debug Bool(a-4>0) ;How is Bool() supposed to know you want an unsigned comparison?
If a-4>0          ;How is IF supposed to know you want an unsigned comparison?


If the comparison 'a-4 > 0' is to be treated as unsigned it should be rewritten as 'a-4 <> 0'.


@little john
a is unsigned so the cmp should treated as such

@demivec

or just use & $ff

Code:
a.a = 2
b.a = a-4   
Debug b

Debug Bool(((a-4) & $ff)>0)
If ((a-4) & $ff) > 0
  Debug "a>0"
EndIf


One thing that's not clear is the documentation, otherwise the OP wouldn't have asked the question
and yes I do think it's a bug as .a and .u are only kind of unsigned it should extend to logic operators


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 10:59 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1021
Location: right here
Everybody talks about the comparison operation, but there is the subtraction before that. Why is it more logical to convert the 4 to unsigned to match the type of .a than to convert the .a to singed? Often in languages both operand are converted to the larger type present, but mixing signedness is usually a bad idea anyway.


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 12:56 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3639
Location: Berlin, Germany
idle wrote:
@little john
a is unsigned so the cmp should treated as such

When a = 2, then treating the expression a-4 as unsigned should result in an error, strictly speaking. Because 2-4 is -2, which is out of the "unsigned" range. Fred decided that the compiler does not raise an error in cases like this, but handles the result of this expression as a signed value instead. That's completely legal.

What should be done in the first place is this: Do not subtract x from an unsigned variable that has value y, when x > y, so that the result would be < 0.
mk-soft wrote:
You have to take care to calculate in the right range.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned variable bug
PostPosted: Thu Aug 15, 2019 1:48 pm 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1847
Location: Germany
It's also good that Fred doesn't intercept this.

In some cases it is normal to expect overflow.
Especially in case of difference calculations of infinite counters, because they generate a normal overflow at some point.

Therefore I was very excited when the function "ElapsedMilliseconds" was changed, because a difference calculation ALWAYS leads to a correct result when an overflow occurs.

Many didn't understand that.

Translated with www.DeepL.com/Translator

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye