BUG ?

Report any Hollywood bugs here
Post Reply
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

BUG ?

Post by PEB »

Code: Select all

Function p_Steps()
	StepVar=StepVar+0.1
	If StepVar=1.1
		DebugPrint("Yep 1")
	ElseIf StepVar=1.2
		DebugPrint("Yep 2")
	ElseIf StepVar=1.3
		DebugPrint("Yep 3")
	EndIf
	If StepVar=1.3
		DebugPrint("Done")
		End()
	EndIf
DebugPrint(StepVar)
EndFunction

StepVar=1
Function p_Test()
	SetInterval(1, p_Steps, 100)
EndFunction

InstallEventHandler({OnMouseUp=p_Test})

Repeat
	WaitEvent
Forever
StepVar=1.1 is recognized, but 1.2 and 1.3 are not.
Any ideas?
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: BUG ?

Post by airsoftsoftwair »

Welcome to the wonderful world of floating point arithmetic :-)

It's not a Hollywood bug... see Wikipedia for more info:
http://en.wikipedia.org/wiki/Floating_p ... y_problems

A simple workaround would be to use Round() on the floating point value. Then it should work.
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

Re: BUG ?

Post by PEB »

Thanks for the explanation!

I don't really need to use a decimal here. And even if I choose to use a variable with a decimal (as in the example), it's really not hard to work around this issue by multiplying by 10 before the variable comparison and then dividing by 10 afterwards. I just thought the observed behavior was odd, and that this might have been a bug of some sort.
Post Reply