angle rounding errors?

Discussion and feedback on Construct 2

Post » Sun Sep 08, 2013 6:53 am

I had an object expression that was doing unexpected thing, so I setup a simple mouseover/text object to check what its angle was ingame.

To my surprise even though I have an object set at 270 deg, it gives a floating point value of 269.9997895409 and one set at 90 deg and the value in game is 90.0002104591.

Is this expected behaviour, maybe something to do with radians to degrees conversion under the hood? even when I set an explicit value to an integer in game or use the round function, this still happens...except for 0 which remains 0.

Oh yeah... I had forgotten about the debugger (which is awesome btw) and confirmed the same thing. Addtionally many position values are also floats. Btw, these are object that are not animated, just moved or rotated by integer values in the layout.

Not sure what the implications of this are? If I am testing an object for an exact integer value of 270 and it is actually 269.9997895409, does C2 round it automatically?
B
11
S
2
G
1
Posts: 108
Reputation: 1,899

Post » Sun Sep 08, 2013 7:31 am

This is expected behaviour. 'Numbers' are generally floats. Either use round(angle) or check if the difference is within some tolerance, abs(270-angle) < 0.01, for example.blackhornet2013-09-08 07:31:26
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Sun Sep 08, 2013 11:27 am

Ok, thanks blackhornet, that is what I thought. I am used to languages where I explicitly declare a variable as a float or an integer , so I guess C2 sees all numbers as floats. Still seems strange that an angle on a static sprite set at 270 degs in the layout should have a floating point value of 269.9997895409 and not 270.0

Btw, was just looking at your midi plugin page for C2...looks right up my alley. Have you or any other users made any examples with it that I could checkout? zendorf2013-09-08 11:34:29
B
11
S
2
G
1
Posts: 108
Reputation: 1,899

Post » Sun Sep 08, 2013 1:36 pm

The engine internally uses everything in radians, and there is a small error introduced by the conversion to radians then back to degrees for the debugger. It's normal and you'll see it in any engine or framework you use that supports floating point numbers. It can happen with ordinary numbers as well, e.g. some calculations like 0.1 * 7 can result in something like 0.69999999998 (this happens at the CPU level - so again will happen with any engine/framework). The usual thing to do is check float values are within a small range like @blackhornet suggested.
Scirra Founder
B
400
S
236
G
89
Posts: 24,549
Reputation: 195,475

Post » Sun Sep 08, 2013 1:50 pm

By the time I finished the MIDI plugin, I was so fed up with MIDI that I moved on to other things MIDI is still a hassle to use. Despite the initial furor, I haven't heard of anyone else actually using it.
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 21 guests