TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Math.Abs: Absolute Value

Use the Math.Abs method from the System namespace to compute absolute numbers.
Math.Abs. An absolute value is not negative. It is the same as the original value but with no negative sign. The Math.Abs method in the .NET Framework provides a tested absolute value function. It deals with certain edge cases.
Example. It is usually trivial to convert variables to positive values from negative values. The Math.Abs method in the base class library in the .NET Framework provides some error-checking that may be useful.

Tip: The C# compiler does overload resolution based on the parameter type. It infers the appropriate method to use.

Overload

Cast: You could cast the variables before passing them as parameters to force the compiler to use a different overload.

Numeric CastsCast, Int

Next: This example shows the absolute value of a negative integer, a positive integer, a negative double and a positive double.

Int, uintDouble

Output: The example program prints 8 numbers. It prints the numbers, and their absolute values.

Console
C# program that computes absolute values using System; class Program { static void Main() { // // Compute two absolute values. // int value1 = -1000; int value2 = 20; int abs1 = Math.Abs(value1); int abs2 = Math.Abs(value2); // // Write integral results. // Console.WriteLine(value1); Console.WriteLine(abs1); Console.WriteLine(value2); Console.WriteLine(abs2); // // Compute two double absolute values. // double value3 = -100.123; double value4 = 20.20; double abs3 = Math.Abs(value3); double abs4 = Math.Abs(value4); // // Write double results. // Console.WriteLine(value3); Console.WriteLine(abs3); Console.WriteLine(value4); Console.WriteLine(abs4); } } Output -1000 1000 (Absolute value) 20 20 (Absolute value) -100.123 100.123 (Absolute value) 20.2 20.2 (Absolute value)
Internals. The Math.Abs method on value types such as int, short and long will use a conditional check that tests if the number if already positive. If the number is positive, it is returned unchanged.
Otherwise, an overflow condition is checked and the unary negation operator is applied, which results in a positive number. If the overflow condition is not useful, you can enhance performance by checking for positive numbers yourself.
For number types such as decimal, double and float, other implementations are called into from Math.Abs, making it a mere wrapper method. For double and float, it is important to use Math.Abs instead of checking for positive yourself.Float

Tip: The external method invoked is likely much faster and simpler. This is often the case in the .NET Framework.

Discussion. Methods such as Math.Abs must check for already-positive numbers before changing the sign. From the hardware perspective, it is easier to flip the sign bit on a signed integer type. You can apply the unary minus (negation) operator.

Tip: You can take the absolute value of a number that is always negative by simply using the unary negation operator.

Summary. We used Math.Abs for a simple, tested absolute value function that works for signed value types. The Math.Abs method accepts decimal, double, Int16, Int32, Int64 and float types, and all keywords that are aliased to those types.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf