It is currently Fri Mar 22, 2019 3:42 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: PB562 More problems in mixing floats with integers
PostPosted: Fri Feb 22, 2019 12:28 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3692
Location: Italy
Original thread viewtopic.php?f=7&t=72322

Code:
Global.l x = 100
; If x < 1.0*1
;     Debug "x < 1.0*1" ; no printing...
; EndIf
; If x < 1.1*1
;     Debug "x < 1.1*1" ; it prints "x < 1.1*1", so 100 is less then 1.1
; EndIf


flo.f = 1.1000000000000001 ; to verify what's happening, let's assign 1.1000000000000001 to a float (1.1000000000000001 is the nearest float to 1.01)

Debug "flo.f = " + Hex(PeekL(@flo.f)) ; the hexadecimal value stored in a float var to represent 1.1000000000000001 is $3F8CCCCD


; the generated asm for the test code

EnableASM
  MOV    dword [v_x],100
; If x < 1.0*1
  MOV    ebx,dword [v_x] ; copy 100 to ebx

; now the compiler "optimizes" the 1.0 * 1 to 1.
; it should keep the zero and consider the expression as float treating the data accordingly

  MOV    eax,1 ; it stores 1 in eax
 
  CMP    ebx,eax ; and then compare that to 100
  JGE   _EndIf2: ; now ebx - eax (100 - 1) is greater than 0, so it jumps
  Debug "x < 1.0*1" ; and this is skipped

; EndIf
!_EndIf2:
; If x < 1.1*1
  MOV    ebx,dword [v_x] ; copy 100 to ebx
 
; again the compiler optimize the 1.1 * 1 to 1.1000000000000001
; this time it does the right thing (so it does two different things for 1.0 and 1.1, a problem in itself ...)
; but in any case insists in not treating the data as float

  MOV    eax,1.1000000000000001 ; it stores $3F8CCCCD in EAX and then treat that as an integer in the comparison (so basically the 1.1 becomes garbage)
 
  CMP    ebx,eax ; compares that to 100
  JGE   _EndIf4 ; now ebx - eax (100 - $3F8CCCCD) is not greater than zero so ...

  Debug "x < 1.1*1" ; it falls into this !
; EndIf
!_EndIf4:
DisableASM


It's a variation on this theme, only worse -> viewtopic.php?f=4&t=59422

_________________
Philosophy is questions that may never be answered. Religion is answers that must never be questioned.

[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

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