Mixing Float and Integer Inequalities?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Mixing Float and Integer Inequalities?

Post by skywalk »

I know this has been mentioned before, but I just got burned by this. :evil:

Code: Select all

#CONST_DOUBLE = 0.9
Define.i x = 0
; BAD: Force Integer comparison!
If x >= #CONST_DOUBLE
  Debug 1
Else
  Debug 2
EndIf
; OK: Force Float comparison!
If #CONST_DOUBLE <= x
  Debug 1
Else
  Debug 2
EndIf
EDIT: Thanks to luis for the author of my current quote line. :D
Last edited by skywalk on Fri Jan 19, 2018 11:17 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Mixing Float and Integer Inequalities?

Post by luis »

The nice thing about standards is there are so many to choose from. ~ unknown
It's by Andrew Tanenbaum, the author of the MINIX OS.
And he's so right. :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Mixing Float and Integer Inequalities?

Post by Bitblazer »

Amoeba will rule us all :P
User avatar
mk-soft
Always Here
Always Here
Posts: 5386
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Mixing Float and Integer Inequalities?

Post by mk-soft »

Small trick

Code: Select all

#CONST_DOUBLE = 0.9
Define.i x = 0
; Force Integer to float comparison!
If 0.0+x >= #CONST_DOUBLE
  Debug 1
Else
  Debug 2
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mixing Float and Integer Inequalities?

Post by skywalk »

Yes, I used that in the past with macros, but my point was, I did not remember the left side was an integer. It would have been a painful bug if it went to release. :cry:
The manual should stress the casting effects, just as it explains order of operations.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Mixing Float and Integer Inequalities?

Post by luis »

It should preferably be fixed instead of just mentioning it in the manual. :)
It's a bug using common sense, and even if sometimes with PB what should be normally considered a bug it's not considered as such since the compiler has its limitations, in this case even Fred agreed.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mixing Float and Integer Inequalities?

Post by skywalk »

Haha yeah, I am simply posting as a sort of muscle memory trick to NEVER get burned again in this case.
I voted for Float elevation in the many prior posts on this topic.
The speed hit does not compare to the propagation of potential logic bugs.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply