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

Bugs will be moved here once resolved.

Post » Thu May 24, 2012 1:59 am

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
cos&Array.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)

Yann2012-05-24 01:59:57
Last edited by Yann on Sun Sep 14, 2014 8:28 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu May 24, 2012 10:43 am

-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.ranma2012-05-24 10:44:47
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Thu May 24, 2012 12:45 pm

@ranma
google works with radian angles, construct 2 with degrees
270 = 3*pi/2 radians
and google says cos(3*pi/2) = 0Yann2012-05-24 12:50:55
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu May 24, 2012 12:49 pm

@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.
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Thu May 24, 2012 12:51 pm

@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)Yann2012-05-24 12:53:18
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu May 24, 2012 12:54 pm

i think that if you pass float number to array.at you shouldn't expect it to work properly. its like calling in pseudocode:
[code]var foo = array('a','b','c');
print foo[1.7];[/code]
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.
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Thu May 24, 2012 12:58 pm

@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 :DYann2012-05-24 12:58:30
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu May 24, 2012 1:18 pm

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!
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Thu May 24, 2012 2:01 pm

@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.Ashley2012-05-24 14:02:57
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,468

Post » Thu May 24, 2012 3:03 pm

Alright thanks (: I think you can safely close this one
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 2 guests