# Getting the sign of a float

4.2.2.stable

### Question

Howdy friends! In the following piece of code, I’m trying to multiply the player’s acceleration by the sign of the normal of the ground they are standing on. For example, if they’re on a slope facing right, the x normal should be positive and the z normal should be zero, and if they’re standing on a slope facing down, the z normal should be negative (I think) and the x normal should be zero. I’m trying to get the sign of the normal values only, not their actual value because that’s a pretty small number. I tried doing this by dividing the normal values by their absolute self to turn them into either positive or negative one and then multiplying the acceleration number by that, but the problem is, if the normal x or z is equal to 0, the acceleration becomes NaN because dividing by zero is illegal. Is there a way I could get the sign of my normals without dividing by zero? Thank you!

How about something like `var sign = 1 if my_float >= 0 else -1`.

There’s a built-in `sign` function, which returns 1 if given a positive number, -1 if given a negative number, and 0 of given either 0 or NaN.

2 Likes

Thank you, this almost works except for one issue, as shown in the screenshot below:

Even though the x normal is 0, the sign function still returns a positive 1, as seen in the print statement at the bottom. Is there a reason for this?

It only returns the sign of zero being positive at the frame where the player steps from a flat surface to a slope, it doesn’t do it if I jump off a flat surface and land on a slope.

I should also add that, in situations where I step onto a north facing slope, I get a -1 as the sign of my normal x. It also says the normal x is -0. What makes a zero negative and how do I prevent this?

used the typed `signf` for floats.

floats are very rarely zero, would it be better to clamp the value?

``ACCEL.x = 2.5 * clampf(floornorm.x, -1, 1)``
1 Like

The clamping works for sure! I’m not exactly sure how, I think the norm x wasn’t actually zero but a very tiny number

1 Like