It is currently Sat Dec 07, 2019 6:03 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Mon Apr 22, 2019 7:18 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
Hi,

for instance when using PB's vectorgraphics library, it can happen that the regarding code contains DesktopScaledX() or DesktopScaledY() with negative arguments. However, the results are strange then:

Code:
; DPI aware = off
Debug DesktopScaledX( 10)  ; 10
Debug DesktopScaledY( 10)  ; 10
Debug DesktopScaledX(-10)  ; 4294967286
Debug DesktopScaledY(-10)  ; 4294967286

Code:
; DPI aware = on (DPI setting on the system is 125 %)
Debug DesktopScaledX( 10)  ; 12
Debug DesktopScaledY( 10)  ; 12
Debug DesktopScaledX(-10)  ; 4294967283
Debug DesktopScaledY(-10)  ; 4294967283

tested with PB 5.71 beta 1 (x64) on Windowsa 10

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Mon Apr 22, 2019 8:12 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Confirm

Code:
; DPI aware = off
x1.i = DesktopScaledX(-10)  ; 4294967286
y1.i = DesktopScaledY(-10)  ; 4294967286
x2.l = DesktopScaledX(-10)  ; -10
y2.l = DesktopScaledY(-10)  ; -10

; Bug as Integer
Debug x1
Debug y1
; Long Ok
Debug x2
Debug y2

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
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: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Mon Apr 22, 2019 8:21 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
That's interesting.
Thanks for the confirmation and the additional finding!

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Sat Apr 27, 2019 3:11 am 
Offline
User
User

Joined: Sun Aug 02, 2015 7:17 pm
Posts: 19
viewtopic.php?f=4&t=71879

Code:
Define dpi = 96*1.25 ;%125

Debug MulDiv_(10, dpi, 96) ; 13

Debug MulDiv_(-10, dpi, 96) ; -13


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 4:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 687
Location: Canada
-10 is Hex $FFFF FFF6

Using @mk-soft's code, we see that

x1.i = Hex $0000 0000 FFFF FFF6 = 4294967286
x2.L = Hex $FFFF FFF6 = -10

so indeed we have a bug : the sign bit isn't extended in the integer representation on an X-64 system, but both values would be correct on a 32-bit system.

_________________
"That's not a bug..." said the programmer. "...It's a feature! "
"Oh! I see..." replied the blind man.


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 5:09 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Who doesn't want to wait or can't
Code:
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  Macro PB(Function)
    Function
  EndMacro
 
  Macro DesktopScaledX(Value)
    ((PB(DesktopScaledX)(Value) << 32) >> 32)
  EndMacro
 
  Macro DesktopScaledY(Value)
    ((PB(DesktopScaledY)(Value) << 32) >> 32)
  EndMacro
CompilerEndIf
 
Debug DesktopScaledX(-10)  ; -10
Debug DesktopScaledY(-10)  ; -10

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
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: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 6:02 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 4:41 am
Posts: 687
Location: Canada
@mk-soft: brilliant !
Thank you also for the idea...

_________________
"That's not a bug..." said the programmer. "...It's a feature! "
"Oh! I see..." replied the blind man.


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 8:35 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
mk-soft wrote:
Who doesn't want to wait or can't
Code:
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  Macro PB(Function)
    Function
  EndMacro
 
  Macro DesktopScaledX(Value)
    ((PB(DesktopScaledX)(Value) << 32) >> 32)
  EndMacro
 
  Macro DesktopScaledY(Value)
    ((PB(DesktopScaledY)(Value) << 32) >> 32)
  EndMacro
CompilerEndIf

; [...] 

My DPI setting is 125 %.

Using your macros, I get these results:
Code:
Debug DesktopScaledX(10)   ; 12
Debug DesktopScaledY(10)   ; 12

Debug DesktopScaledX(-10)  ; -13
Debug DesktopScaledY(-10)  ; -13

This is not correct, since the absolute values of the results should be the same.
Also, the macros can be simplified a bit: Only two macros are needed. :-)

This is my suggestion for a workaround:
Code:
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
   Macro DesktopScaledX (Value, Function=DesktopScaledX)
      (Sign(Value) * Function(Abs(Value)))
   EndMacro
   
   Macro DesktopScaledY (Value, Function=DesktopScaledY)
      (Sign(Value) * Function(Abs(Value)))
   EndMacro
CompilerEndIf


;-- Demo
Debug DesktopScaledX(10)   ; 12.0
Debug DesktopScaledY(10)   ; 12.0

Debug DesktopScaledX(-10)  ; -12.0
Debug DesktopScaledY(-10)  ; -12.0

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 9:14 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Sorry John,

The result is already correct. Only the front bits are missing.
Code:
Debug -10 * 125 / 96 ; = -13
Debug Round(-10.0 * 1.25, #PB_Round_Nearest)

x2.l = DesktopScaledX(-10)  ; -13
Debug x2

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


Last edited by mk-soft on Fri May 03, 2019 9:55 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 9:48 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
mk-soft wrote:
The result is already correct. Only the front bits are missing.
Code:
Debug -10 * 125 / 96 ; = -13
Debug Round(-10.0 * 1.25, #PB_Round_Nearest)
Sorry, but you are wrong.
This is not a question about bits, but about graphics.

If there is a distance of say 10 pixels on the X axis, then with DPI scaling we might want to get the result of DesktopScaledX(10) or of DesktopScaledX(-10). Wetheter the sign of the argumewnt is positive or negative depends on the direction, i.e. wheter we go from left to right or from right to left. But the distance is always 10 pixels in this case. Scaling does not depend on the direction from where we calculate the distance. if the distance (i.e. the absolute value of the argument) is the same, then the result of the scaling must also be the same. This is a matter of course. So if an original distance of 10 pixels after scaling sometimes is 12 and sometimes is 13, then there is something wrong, obviously.

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 9:51 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Then calculation is sometimes not correct in the 32Bit version as well.

But my conversion from signed long to signed quad is correct.
I am absolutely convinced of that.

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
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: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 10:01 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
mk-soft wrote:
Then calculation is sometimes not correct in the 32Bit version as well.

Then that has to be fixed, too.
Again: The distance between 2 points is always the same, regardless whether it is measured from left to right or from right to left. Any result that is not in accordance with this basic geometrical fact cannot be correct. Period.

My suggested workaround posted above yields results with equal absolute values for both positive and negative arguments:
Quote:
Code:
;-- Demo
Debug DesktopScaledX(10)   ; 12.0
Debug DesktopScaledY(10)   ; 12.0

Debug DesktopScaledX(-10)  ; -12.0
Debug DesktopScaledY(-10)  ; -12.0

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 10:07 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
mk-soft wrote:
But my conversion from signed long to signed quad is correct.
I am absolutely convinced of that.

We have the floating point numbers 12.5 and -12.5 here.
In your calculation, 12.5 is rounded to 12, and -12.5 is rounded to -13. That's the problem.

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 10:09 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Perhaps perfect solution
Code:
Procedure Scaled(Value.i, factor.d)
  If value < 0
    ProcedureReturn Round(Value * factor, #PB_Round_Up)
  Else
    ProcedureReturn Round(Value * factor, #PB_Round_Down)
  EndIf
EndProcedure

For i = -96 To 96
  Debug "" + i + " = " + Str(Scaled(i, 1.25))
Next

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
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: [5.71 beta 1] DesktopScaledX/Y with negative arguments
PostPosted: Fri May 03, 2019 10:35 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3715
Location: Berlin, Germany
mk-soft wrote:
Perhaps perfect solution

PureBasic's Round() function has the three options:
- #PB_Round_Up (= "round towards positive infinity", a.k.a. Ceil())
- #PB_Round_Down (= "round towards negative infinity", a.k.a. Floor())
- #PB_Round_Nearest

Mathematically speaking there are more rounding modes.
An important rounding mode is "round towards zero", a.k.a. Trunc().
This is what the procedure in your previous post does.

However, this is also what PureBasic's built-in functions Int() and IntQ() are doing. :-)
Code:
Procedure Scaled(Value.i, factor.d)
  If value < 0
    ProcedureReturn Round(Value * factor, #PB_Round_Up)
  Else
    ProcedureReturn Round(Value * factor, #PB_Round_Down)
  EndIf
EndProcedure

Macro ScaledNew (Value, factor)
   Int(Value * factor)
EndMacro


For i = -96 To 96
  Debug "" + i + " -> " + Str(Scaled(i, 1.25))
  Debug "" + i + " -> " + Str(ScaledNew(i, 1.25))
  Debug ""
Next

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


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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