Math question

Monkey Forums/Monkey Beginners/Math question

navyRod(Posted 2014) [#1]
Wondering why this is okay with the step factor or .000001 all the way up to 32 and then apparently gets tired after that ?


Guess if I was a computer I would get tired too. If I use a stepping val of .00001 it goes all the way up to 360 but with the stepping at .000001 it gets ?? after 32


navyRod(Posted 2014) [#2]
take that back .. can't get to 360 with a step of .00001 -- can only get to 256 and hangs up at 257 and beyond


navyRod(Posted 2014) [#3]
think I figured it out with some print debug statements.

Apparently at a certain point a floating point number + the step value (being so small) when added does not get a new converted value that is different than the original and gets stuck in an infinite loop at that point .

Does this sound correct ?


consty(Posted 2014) [#4]
This code actually crashed TED
Strict
Function Main:Int()
	For Local i:Float = 0 To 1 Step 0.000001
		Print(i)
	Next
		
	Return 0
End


Here also another example
Strict

Function Main:Int()
	Local count:Int = 0

	For Local i:Float = 0 To 1 Step 0.001
		Print(count + " " + i)
		count += 1
	Next
		
	Return 0
End

This in reality means that this code will actually run 1000 times
Final debug output: 1000 0.99999070167541504
This means that the step can be calculated like this: 1/1000 = 0.001





In the first example the loop would run 1 million times.
This code as well is a pain to run in debug mode and crashes. I also tried it release mode and still crashed.
Strict

Function Main:Int()
	For Local i:Float = 0 To 1000000
		Print(i)
	Next
	Return 0
End


Perhaps that would be something that Mark could look and see what is it. Is it a bug? It is something else?

P.S. TED looks like it's stuck but actually if you terminate monkeygame process in the task manager you will be OK.


Gerry Quinn(Posted 2014) [#5]
Looks like it fails to increment at all when the step is too small. Desktop target uses 32-bit floats by default. There's a config setting you can alter:
#CPP_DOUBLE_PRECISION_FLOATS = True

That should fix the issue. An alternative is to simply use ints for loops:

For Local i:Int = 0 Until 360 * 100000
Local angle:float = Float( i ) * 0.00001
Next

Though for most applications it looks like you are engaging in some serious calculationary overkill...