Unexpected behaviour when changing a property from a local function (SOLVED)

I have this local function:

local function pluspointonetimer(self, url, property)
	print("increasing self.stage")
	print(self.stage) -- prints 1.8
	self.stage = self.stage+0.1 --advances one stage
	print(self.stage) -- prints 1.9
	self.delayed =1 
end

and this in “update”:

	if self.stage== 1.8 and self.enter then
		if self.onceas == nil then
			msg.post("#modemconnect", "play_sound" )
			msg.post("#dialtone", "stop_sound" )
			go.animate("#", "delayed", go.PLAYBACK_ONCE_FORWARD, 0, 1, 8, 0, pluspointonetimer)
			self.onceas = 1
		end
	end
	if self.stage== 1.9 then
		print("download complete!") --this never prints
	end

I get all the correct print outs from the local function, but i never get the printout from “self.stage==1.9”!!

Any reason that this happens?

Okay… this is super weird!!

This code works fine (with no change made to the local function!)

	if self.stage== 1.8 and self.enter then
		if self.onceas == nil then
			msg.post("#modemconnect", "play_sound" )
			msg.post("#dialtone", "stop_sound" )
			go.animate("#", "delayed", go.PLAYBACK_ONCE_FORWARD, 0, 1, 8, 0, pluspointonetimer)
			self.onceas = 1
			self.stage = 1.9
		end
	end
	if self.stage== 2 then
		print("download complete!")
	end

It is a floating point value so it is likely not exactly 1.9. Print it in update with all decimals.

See this thread: False equation?!

2 Likes

You should probably switch to integers, use stages 18, 19, 20 instead.

2 Likes

Also, read about floating point numbers. As you noticed, it comes up more often than you’d think, and it’s a good thing to understand.

read about floating point numbers

Read more about how they were sent here by Satan to make programming impossible?

No but seriously thanks everyone. I will have a look because it seems really interesting.

3 Likes

They say the devil is in the details.

1 Like

…actually I’ve always been really interested in how computers work. When I realised that computers can’t really generate random numbers, I spent a lot of time thinking about the nature of the universe.

On a related note, this is quite neat.

1 Like

Yes! And you know the weird thing? Humans are also very bad at generating random numbers and generally use similar processes to computers to think of something random. In fact, they do it worse.

1 Like