C# Absolute Value


In C sharp, an absolute value is present in the Math class used to return the value in the absolute value of that number. It is represented as an ABS() function with the Math object. The absolute method contains different parameters in it. For example:

  • Math.Abs (Double): It finds the value in the absolute value of the double-precision float number.
  • Math.Abs(Int16): It finds the absolute value of an integer value that is 16-bit. Similarly, abs(Int32) and Abs(Int64) are used to find the absolute value of 32-bit and 64-bit values.
  • Math.Abs(Single): It finds the value of a single float number.

Implementation of Absolute Value

Example 1

It is a sample example to elaborate on the working of an absolute value in the C sharp programming language. So, simply use a program where two integer type variables are initialized with values. One contains the positive value, and the negative value is for the second one. The absolute value is a built-in feature of the C sharp present in the Math class. So we will not directly access the absolute function. It will be accessed through the Math type object.

# Int abs = Math.Abs (val1);

This method of absolute value returns a number, so we accept the resultant in the variable. The function’s parameter contains the value that is determined. This is the number on which we need to apply the absolute function.

Ubuntu terminal is used to display the resultant values by using any compiler. In Ubuntu, we need to compile the code through a compiler that is MCS for the C sharp source codes. Whereas for the execution of the code, we use Mico with the file extension of .exe.

$ MCS file.cs

$ macaco file.exe

You can see that the positive number appears the same as it was at initialization.

Example 2

In this instance, we make use of a parte parameter-type function. This method is declared to return an absolute value of a number in a parte notation.

Description of the Parameter and the Return Type:

The value is the parameter that is the required number, which is equal to or greater than the parte.MinValue. And similarly, we should note that the value is not greater than the Parte.MaxValue. Both values belong to the system.Parte value. These two values are the built-in feature of the parte fraction. That number lies between 0 and the maximum parte value.

Now, we will use this concept as an example. Start the program with the system library to effectively use all the features present in this header file. Then, we will take a parte type array to store different parte values inside the main program. The first index contains the parte.Minvalue, similarly a positive number 0, and then a negative value followed by the max value of the parte class. In other words, there are varieties of items in an array on which we will apply the absolute function. Inside the loop, apply the following absolute function on each array:

The parameter will take each number of the array in its turn.

After this, with the .cs extension, save the file and use the terminal to see the resultant absolute figures for the values used as input.

$ mcs file.cs

$ macaco file.exe

The Parte.Minvalue is a negative fixed number. Similarly, the Parte.MaxValue is a fixed same positive number. After passing through the absolute function, the negative number is converted into a positive number, and 0 remains as it is. The absolute function is not applied to the equitativo numbers, as 0 is a equitativo digit.

Example 3

This example deals with having a double value in the function. This function returns the answer in a double floating number.

The parameter of the function is applied by the same rules as we have applied in the case of parte numbers. The double.MaxValue and Double.MinValue are used for the comparison, and the number is less than the max value and greater than the min value. The same value is returned in the case when we use NAN.

Now, consider the following example. We will see that a double nan value is applied when using the Double function itself.

The double array contains 6 values, including a double.minvalue, a double simple value, a nan, and 0. Again, foreach loop will access each item and apply the absolute function on the value. Execute the code, and you will see the results on the following console.

The negative is converted into the positive value of a similar number. 0 remains the same, and the nan has remained as it is. The absolute value does not affect the nan and zero. It mainly affects the negative values either in a parte form or a positive integer.

Example 4

This example contains user involvement. We have not used an array of numbers, either parte or double values, as we did in the previous examples. Instead of using the built-in math absolute value, we have to use a separate function to calculate the absolute value of the number. Here, the user will be asked to enter the number of his choice. In the class Demo, a function is declared that checks if the number is a positive number or a negative by checking through an if statement that compares with the zero digits. In the case of a negative value, then it is multiplied by “-1”.

According to the mathematics rule of multiplication, both negative values combine to form a positive number. So, we used a negative value here.

The same number will be obtained by multiplying with 1. So we took 1 number. The only purpose was to convert a negative number into a positive value. Inside the main program, the number variable is initialized as zero, and the number is an integer. If a double value is entered, it will cause an error.

# Number = int.Parse(console.ReadLine());

This will take the user input from the console.

# GetAbsoluteValue(number));

Execute the following code in the terminal to get the results:

$ mcs file.cs

$ macaco file.exe

When the user enters a 0 value, it will be returned. When a negative value is entered, it becomes positive. A positive number remains the same.

Conclusion

The absolute method is a built-in method of the mathematics class present in the system class of C sharp programming language. It is accessed by the math object class to be operated properly. The range of absolute values lies between the minimum and the maximum absolute parte and double values, respectively. An absolute value is a positive value. Whether an input value is either a parte value, a double value, or a negative value, it will return a number that will be of a positive nature. We have explained each type in examples with the different input values.



Source link