A quick way to ensure that -0 returns as 0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AfterCadaver

Sometimes when I use round(), I get -0 because of division by negative integer. Does anyone know any quick and simple ways to get just 0 when doing this in general?

Can you give us an code example?

That smells like a bug. -0 is a surreal number that has not to exist.

juppi | 2020-06-27 10:51

One possible option is to use int(round(x)) instead of round(x).

For example, print(int(round(1.0/(-3)))) displays 0, whereas print(round(1.0/(-3))) displays -0.

haydenv | 2020-06-27 13:33

:bust_in_silhouette: Reply From: selamba

You can use abs()

As in abs(-0) == 0 is always true

selamba | 2020-06-27 15:57

This answer produces the most readable code.

DDoop | 2020-06-27 17:02

What if I am working with a Vector2()?

If one of the values–either x or y–is -0, what are any quick/ simple ways to covert a vector for example?

AfterCadaver | 2020-06-29 05:48