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
Post a Comment