Is there a number one module function in C#?


Example: modulus of -1 (|-1| = 1). I've done some research on the internet but all I can find is the division module (%). I'm trying to get around the common way of passing a negative number to a positive one, which would be to check to see if the number is negative or not before multiplying by -1.


For me, module in programming has always been what this operator does, so the rest of the description is contradictory, but what it described seems to be that it wants to get the unsigned value, so it would be the Abs() method. As it takes the absolute value, that is, without sign, it does not matter if it is positive or negative, its result will be a positive. This method does exactly the check if it is positive or not before changing the sign and does the change of sign (it doesn't use neither if , nor multiplication which are inefficient, you can see the source code ).

Math.Abs(-1) //resulta em 1

I put it on GitHub for future reference .

Scroll to Top