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 Mono with the file extension of .exe.

$ MCS file.cs

$ mono 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 decimal parameter-type function. This method is declared to return an absolute value of a number in a decimal 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 decimal.MinValue. And similarly, we should note that the value is not greater than the Decimal.MaxValue. Both values belong to the system.Decimal value. These two values are the built-in feature of the decimal fraction. That number lies between 0 and the maximum decimal 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 decimal type array to store different decimal values inside the main program. The first index contains the decimal.Minvalue, similarly a positive number 0, and then a negative value followed by the max value of the decimal 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:

# Math.Abs(value));

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

$ mono file.exe

The Decimal.Minvalue is a negative fixed number. Similarly, the Decimal.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 neutral numbers, as 0 is a neutral 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 decimal 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.

# Double.NAN;

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.

$ mono file.exe

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 decimal form or a positive integer.

Example 4

This example contains user involvement. We have not used an array of numbers, either decimal 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”.

# Number = number * 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

$ mono 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 decimal and double values, respectively. An absolute value is a positive value. Whether an input value is either a decimal 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.



from https://ift.tt/IZHkhnx

Post a Comment

0 Comments