0

How to convert negative number to positive without abs and if

python

21st Sep 2017, 6:37 AM
anwar tan
anwar tan - avatar
6 Respuestas
+ 12
Your requirement is strange but I believe basic arithmetic works:- ☑ 0 - negative number = positive number ☑ -1 * negative number = positive number @Sergey suggestion is same with the latter approach above.
21st Sep 2017, 7:22 AM
Zephyr Koo
Zephyr Koo - avatar
+ 8
absolute value is the same as sqrt(x**2)
21st Sep 2017, 1:11 PM
m abrate
m abrate - avatar
+ 5
x=max(-x,x) it will keep positive, and change the sign of negative, it it works as abs(). :)
21st Sep 2017, 11:05 AM
yuri
+ 4
You can use number-2*number.
21st Sep 2017, 7:17 AM
Sergey Antonyuk
Sergey Antonyuk - avatar
+ 4
And another, rather silly way, usually applied on variable, -n, where n contains a negative number ;)
21st Sep 2017, 10:32 AM
Ipang
+ 1
thanks for answers all
21st Sep 2017, 2:44 PM
anwar tan
anwar tan - avatar