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

0 favourites
  • 10 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • 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


    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

  • -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.

  • ranma

    google works with radian angles, construct 2 with degrees

    270? = 3*pi/2 radians

    and google says cos(3*pi/2) = 0

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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)

  • 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.

  • ranma

    hmmm yeah probably but it makes the expression far less cute ;_;


    instead of


    yeah ok... you're right :D

  • 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!

  • 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.

  • Alright thanks (: I think you can safely close this one

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)