# PureBasic Forum

 It is currently Thu Feb 20, 2020 2:07 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: [Done] PB562 More problems in mixing floats with integersPosted: Fri Feb 22, 2019 12:28 pm

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3695
Location: Italy

Problem

Code:
Global.l x = 100

; to verify what's happening, let's assign 1.1000000000000001 to a float (1.1000000000000001 is the nearest double to 1.10)

flo.f = 1.1000000000000001 ; it is converted to the float 1.10000002384186

Debug flo ; 1.10000002384186

; the hexadecimal value stored in a float var to represent 1.10000002384186 is \$3F8CCCCD

Debug "flo.f = " + Hex(PeekL(@flo.f)) ; \$3F8CCCCD

; the generated asm for the test code above is

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 Str(x) + " is < 1.0*1" ; and this is skipped, OK

; 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 using a double
; 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 floating point value

; it stores \$3F8CCCCD in EAX and then treat that as an integer in the comparison (so basically the 1.1 becomes garbage)
MOV    eax,1.1000000000000001 ; the double is converted to the float 1.10000002384186, and from there to \$3F8CCCCD

CMP    ebx,eax ; compares that to 100

JGE   _EndIf4 ; now ebx - eax (100 - \$3F8CCCCD) is not greater than zero so ...

Debug Str(x) + " is < 1.1*1" ; it falls into this, NOT OK !!!
; EndIf
!_EndIf4:
DisableASM

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

_________________

Last edited by luis on Mon Feb 17, 2020 10:14 pm, edited 1 time in total.

Top

 Post subject: Re: PB562 More problems in mixing floats with integersPosted: Tue May 14, 2019 8:17 pm

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1732
Definitely a bug, and thank you for investigating it.

Bugs like this in fundamental features like type casting worry me the most!

_________________
On GitHub: PB Includes - IDE Tools - Color Themes

Top

 Post subject: Re: [Done] PB562 More problems in mixing floats with integerPosted: Wed Jan 29, 2020 10:34 pm

Joined: Fri May 17, 2002 4:39 pm
Posts: 13893
Location: France
Fixed.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite