What is the most efficient (fastest) way to get a lower bound of zero?
Math.max(0, x) will work, returning 0 for any negative value of x. However, my experience with Math.* is that there often is a far more performant trick to do it faster. Anyone know of any in this case?
Bitwise tricks are fine; I always like finding real uses for those operators. Anything that will return false, if x is negative, also is fine.
Edit
To clarify, if x is greater than 0, I want that value. So I can’t just do x < 0, as that will only give me true, not x.
One way with your requirements is:
given that you are ok with the expression evaluating to
falsewhenxis 0 or negative. If you just wantfalsewhenxis negative, you can do:Examples: