Solved: sign function

0 favourites
  • 11 posts
From the Asset Store
Math Signs Game
$8 USD
50% off
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported
  • sign(x)

    return -1 or 1

  • Or you could use this instead...

    abs(x) / x

  • Or...

    abs(x) / x

    and then 0 came x)

    for that I'd use the function object, or directly (x=0?0:abs(x)/x), but I agree that a sign function returning -1, 0 or 1 would be kind of nice for readeability.

  • Aphrodite - very good point!! You've probably just saved me some pain in the future....

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • But how is it useful?

    Can you show where it would come in handy?

  • newt it's useful in tons of cases. Classic case, when you want to set a certain variable based on the x-component of movement, you'd use sign(x) on the vector and multiply that by a constant to create a multiplier for an animation speed or something.

  • Well its not supported in js by all the browsers, so I doubt it would get added, as conditionals handle it fairly well.

    Exceptional examples help make exclusive expressions.

  • +1 for me as well. I've had situations where it would've been handy to simplify some expressions.

    Construct Classic had it and it's true that it could be done with existing expressions, but the same can be said for lerp, clamp, abs and probably others.

    I also don't agree that it isn't supported everywhere, it can be done in js with just a few comparisons so it would work everywhere. Anyway, it isn't absolutely needed but I imagine it is as easy to add as qarp, cubic and cosp.

  • I agree with r0j0hound, about the fact that it is not because it is easy that it shouldn t be included:

    First: readeability, just reading Sign() is explicit enough, contrary to the other expressions we can set up for it

    2nd: I had more trouble doing it that doing a lerp with expressions, because of the 0! And it is easy to forgot for a non mathematician guru than 0/0 does not exist (lerp does not need any conditionnal, it is a simple calculation, while Sign is not a calculation, but an evaluation at the end)

  • Well makes sense -0 & +0 are 0, I don't see the point

  • SOLUTION:

    javascript:

    function MathSign(number)
    {
    	return number?number<0?-1:1:0;
    }
    [/code:2ydogslp]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)