[r91] Array.At and cos(270) or cos(-270)

Forum Home Forum Home > Construct 2 Development > Bugs > Closed bugs
 Post Reply Post Reply
Author
11,882 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Topic: [r91] Array.At and cos(270) or cos(-270)
    Posted: 24 May 2012 at 3:03pm
Alright thanks (: I think you can safely close this one
Back to Top

Scirra Developer
81,388 Rep
Post Options Post Options   Quote Ashley Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 2:01pm
@ranma is right and because computers are not perfectly precise the result is actually -1.8369701987210297e-16. The confusing thing here is that when adding floats to strings, Construct 2 rounds numbers to about the nearest billionth before adding to the string. The motivation for this is again floating point inaccuracy: if you say "Every tick, add 0.3 to a variable" and set a text object to the result, due to rounding errors eventually you'll get something like 2.999999999999997. Often due to small sized text objects which cannot fit a lot of digits this means the number disappears completely, and we get users reporting it as a "bug". So with a very very small amount of rounding, it continues to appear as expected and you get "3". In this case the result is such a small number you just get "0".

The exception is the str() expression which does not do any rounding. If you display str(cos(270)), you'll see the real answer.

The Array object does floor() on floats passed as indices, so presumably -0.000000000000000183... is floored to -1, which returns 0.

Solution: round the indices yourself. This is standard practice in programming - whenever you want to use a floating point number as an integer, it's best to round it or floor it explicitly so you get better behavior.

Edited by Ashley - 24 May 2012 at 2:02pm
Back to Top
8,224 Rep
Post Options Post Options   Quote ranma Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 1:18pm
also remember that round, floor and ceil functions round a little differently, round simply rounds, floor rounds down, ceil rounds up. just in case, hope that helps. cheers!
Back to Top
11,882 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 12:58pm
@ranma
hmmm yeah probably but it makes the expression far less cute ;_;
lvl.At(loopindex("x")+round(cos(a)),loopindex("y")+round(sin(a)))
instead of
lvl.At(loopindex("x")+cos(a),loopindex("y")+sin(a))
yeah ok... you're right :D

Edited by Yann - 24 May 2012 at 12:58pm
Back to Top
8,224 Rep
Post Options Post Options   Quote ranma Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 12:54pm
i think that if you pass float number to array.at you shouldn't expect it to work properly. its like calling in pseudocode:
var foo = array('a','b','c');
print foo[1.7];

what do you expect out of that? :) there's your problem. I think you must floor (or ceil - depending on what you want to achieve) your cos() function before passing to array.at() and it will work like a charm.
Back to Top
11,882 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 12:51pm
@ranma
Yeah you're probably right on one thing, I probably doesn't get exaclty 0 in the expression.
In theory cos(270) should return 0 (because it's the downward 90 degree angle so it projects on 0 in the abscisses)
But I know for instance that in C++ you don't get perfect number. I guess in js it must be the same, but as the display in the text object is ok, I think there's already some kind of rounding somewhere along the line.
Except in Array.At() expression (and maybe in others)
That's why I think it's a bug or at least an unexpected behavior. (doesn't happen for cos(90) for instance)

Edited by Yann - 24 May 2012 at 12:53pm
Back to Top
8,224 Rep
Post Options Post Options   Quote ranma Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 12:49pm
@Yann not a math expert myself, but i'm convinced you're right though :) anyway, as for the array, did my explanation solve your problem? remember that array.at() takes an integer, not a float.
Back to Top
11,882 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 12:45pm
@ranma
google works with radian angles, construct 2 with degrees
270° = 3*pi/2 radians
and google says cos(3*pi/2) = 0

Edited by Yann - 24 May 2012 at 12:50pm
Back to Top
8,224 Rep
Post Options Post Options   Quote ranma Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 10:43am
-1.8369701987210297e-16

this is your cos(a)

how do you expect to get the value from array.at(-1.8369701987210297e-16) ? :D

the behavior you get is perfectly normal i think.

but, google says that cos(270) = 0.984381951.

Edited by ranma - 24 May 2012 at 10:44am
Back to Top
11,882 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 24 May 2012 at 1:59am
When I use a cos(270) or cos(-270) in an Array.At expression
I don't get the expected result unless I round them.

Anyway, a capx is better than a thousand words
http://dl.dropbox.com/u/23551572/C2/Report/bugs/cos%26Array.capx

I discovered this weird bug by playing with a terrain generator (from Intrepid) to get the 4 surrounding cell of the terrain array through a loop instead of 4 events (was too lazy)



Edited by Yann - 24 May 2012 at 1:59am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down