tags:

views:

196

answers:

4

Similar in concept to Math.Abs() - I'm looking for a function that when given a positive integer will return the same integer. If given a negative, will return zero.

So:

f(3) = 3
f(0) = 0
f(-3) = 0

Yes, this is simple enough to write on my own but I'm wondering if the .NET Math class already has this built in or if the same can be achieved by cleverly chaining a few Math.* calls?

+18  A: 

This seems to be what you want, no?

Math.Max(0, num);
Ron Warholic
+16  A: 

It's called Math.Max :)

Math.Max(0, x)

Bubblewrap
+8  A: 

I think

Math.Max(0, x)

is what you want.

Thomas
+2  A: 

It looks like Math.Max is the way to go, but this would work too... ;)

(num + Math.Abs(num)) / 2
ileff
If you're ok with potential overflow on the addition
Brian
+1 Interesting take! It's the type answer i expected initially.
Paul Sasik