Check the sign of a number

Does Julia have a simple way to check the sign of a number? Something like:

sign(-4) = -1
sign(4) = 1

That function exists with that name, no?

5 Likes

You can try searching the docs, e.g. https://docs.julialang.org/en/v1/search/?q=sign, they are sometimes not great but here the third answer is to the function https://docs.julialang.org/en/v1/base/numbers/#Base.signed and the fourth is to the section https://docs.julialang.org/en/v1/manual/mathematical-operations/#Sign-and-absolute-value-functions-1

1 Like

That was strange. Somehow my code threw and error when I tried, and I just assumed it it didn’t exist. I should have checked the simple case…

1 Like

Note that if you try this on a vector, e.g. sign([1;4]) then you’ll get an error since the correct syntax would be sign.([1;4])

3 Likes

If the code you tried is exactly what you posted, that was not comparison (==) but assignment (=). That would explain the errors you saw.

5 Likes