YoYoGames / GameMaker-Bugs

Public tracking for GameMaker bugs
21 stars 8 forks source link

number_copy_sign #6894

Open coyo-t opened 1 month ago

coyo-t commented 1 month ago

Is your feature request related to a problem?

example: when tracing a line through a grid you need the "step" for each axis of the direction vector this can be written as var step_x = direction_x >= 0 ? +1 : -1, which is long but fine enough. (sign(direction_x) can't be done as having a step value of 0 can result in problems/deadlocks)

if you want to store the previous grid xy while still being able to compare the two in a consistent manor, you could initialize it as var pev_x = infinity * -step_x, or var pev_x = step_x >= 0 ? -infinity : +infinity both are not as clear to read, as well as all 3 having more chances for odd/harder to track down bugs/errors due to mistypings

Describe the solution you'd like

either copy_sign(from, to) or number_copy_sign(from, to). returns the 2nd argument with the sign bit of the 1st argument (for doubles anyway)

IE, the above could be rewritten as var step_x = copy_sign(direction_x, 1) and var pev_x = -copy_sign(step_x, infinity), which i think is easier to read? for me, anyway. has much less "surface area" for mistyping bugs as well.

Describe alternatives you've considered

none that werent already listed or are obvious.

Additional context

No response

sukus21 commented 1 month ago

Edit: I missed the part where 0 should be treated as having a sign of 1, sorry.

Would a function like this help:

function number_copy_sign(from, to) {
    return sign(from) * abs(to);
}