I am looking for Math.Abs(ulong,ulong) with return type ulong. But it seems Microsoft only implemented it for long, int and so on. Is there another fast way to do it?
Sorry, need to correct:
Math.Abs(ulong - ulong)
So it can get negative, and be out of the range of a long.
abs() method returns the absolute (Positive) value of a int value.
In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it.
The absolute value of a Decimal is its numeric value without its sign. For example, the absolute value of both 1.2 and -1.2 is 1.2.
Abs() method in C# is used to return the absolute value of a specified number in C#. This specified number can be decimal, double, 16-bit signed integer, etc.
Unsigned long values are always positive, as they do not contain a sign. As such, Math.Abs
would make no sense for ulong
.
Given your new question, you can use:
ulong difference = first > second ? first-second : second-first;
This will give you the difference between the two values, which is effectively the absolute value of the result you'd get by subtracting the two values as if they were signed.
To avoid going out of range I think you want something like this:
a > b ? a-b : b-a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With