How to make sure a value is positive?

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

I knew a function that did this.

Depends. The first answer, (using abs) will make the value positive wether it is negative or positive already. The second answer (is value > 0) will check to see if the value is positive or not. Either one could be right depending on how we interpret your question.

ka0s420 | 2019-09-22 15:00

1 Like
:bust_in_silhouette: Reply From: gmaps

abs(value) is the way
Docs

This works but Godot wants this number to be a float. so it will complain about NARROWING_CONVERSION. It’s not a big problem but I can’t help but wonder if there is a method specifically for integers

monsterousoperandi | 2022-03-27 17:25

1 Like
:bust_in_silhouette: Reply From: Manuel

check is > than 0

:bust_in_silhouette: Reply From: Nate.Ma

I kind of feel like and idiot now I spent an hour making this equation:
sqrt(pow(x, 2) to do the thing I could have done with abs(value)

1 Like

Godot 4.2.1

According to the docs for integers you may use absi function, same as absf for a float

See the abs func description

1 Like