I came up with only one way:
if(x > 0) { x -= x*2; } else x -= x*-2; Is it possible without that if-else , in one line?
I came up with only one way:
if(x > 0) { x -= x*2; } else x -= x*-2; Is it possible without that if-else , in one line?
Everything is easier than it seems. x = -x; which is equivalent to x = -1 * x;
Condition
if (x > 0) { x -= x * 2; } else { x -= x * -2; } can be represented as:
x -= x > 0 ? x * 2 : x * -2; or so:
x -= x * ((x > 0) ? 2 : -2); Condition
if (x > 0) { x -= x * 2; } else { x -= x * -2; } What about x = -x ?
Source: https://ru.stackoverflow.com/questions/909500/
All Articles
x -= Math.abs(x*2);- entithat-xdoes not allow to use Zarathustra? - MBo