ludic/docs/language/math/math-sign.md

924 B

id name category kind tokens sig tip order ns member
math-sign Math.sign math namespace-method Math.sign Math.sign(x) -> int The sign of a value as -1, 0, or 1. 4 Math sign

Returns -1 when x is negative, 1 when it is positive, and 0 when it is exactly zero. It works on both int and fixed values, since zero and sign are the same in Q16.16. Multiply a step by Math.sign(dx) to move one unit toward a target, or read the sign of a velocity to decide which way a sprite faces.

Parameters:

  • x — the value to test
program FaceTarget {
  state FaceTargetState {
    x: int = 8
  }
  let target: int = 20

  handler StepToward(face_target_st: mut FaceTargetState) phase Update {
    face_target_st.x = face_target_st.x + Math.sign(target - face_target_st.x)   # +1 while target is to the right
  }
}