java - Inline Conditional Negation -


is there inline way conditionally negate value or expression without having use conditional evaluation such following?:

result = (condition) ? value : -value; result = value * ((condition) ? 1 : -1); 

i'm working in javascript, i've been wondering same other languages use it.

you can (bool - !bool * 1) * value. (bool - !bool * 1) returns 1 true, -1 false. method overkill; stick original readable code. c/c++ can away macro (but careful bool evaluated twice).

edit: (bool - !bool) * value works in javascript , php (haven't tested others). guess added * 1 in case couldn't figure out supposed int :p

i'm not sure faster (though believe arithmetic expressions faster branches), , others have said, don't need worry speed. you're not saving on many characters either :p


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -