Mathematics in C#: Arithmetic 

Absolute Values 
The decimal numeric system counts from negative infinity to positive infinity. This means that numbers are usually negative or positive, depending on their position from 0, which is considered as neutral. In some operations, the number considered will need to be only positive even if it is provided in a negative format. The absolute value of a number x is x if the number is (already) positive. If the number is negative, its absolute value is its positive equivalent. For example, the absolute value of 12 is 12, while the absolute value of –12 is 12. 
To get the absolute value of a number, the Math class is equipped with a method named Abs, which is overloaded in various versions. Their syntaxes are: public static sbyte Abs(sbyte value); public static short Abs(short value); public static int Abs(int value); public static float Abs(float value); public static double Abs(double value); public static long Abs(long value); public static decimal Abs(decimal value); This method takes the argument whose absolute value must be fond. Here is an example: using System; class Program { static int Main() { int number = 6844; Console.WriteLine("Original Value = {0}", number); Console.WriteLine("Absolute Value = {0}\n", Math.Abs(number)); return 0; } } This would produce: Original Value = 6844 Absolute Value = 6844 Press any key to continue . . .
Consider a floatingpoint number such as 12.155. This number is between integer 12 and integer 13: In the same way, consider a number such as –24.06. As this number is negative, it is between –24 and –25, with –24 being greater. In arithmetic, the ceiling of a number is the closest integer that is greater or higher than the number considered. In the first case, the ceiling of 12.155 is 13 because 13 is the closest integer greater than or equal to 12.155. The ceiling of –24.06 is –24. To support the finding of a ceiling, the Math class is equipped with a method named Ceiling that is overloaded with two versions whose syntaxes are: public static double Ceiling(double a); public static decimal Ceiling(decimal d); This method takes as argument a floatingpoint number of variable whose ceiling needs to be found. Here is an example: using System; class Program { static int Main() { double value1 = 155.55; double value2 = 24.06; Console.WriteLine("The ceiling of {0} is {1}", value1, Math.Ceiling(value1)); Console.WriteLine("The ceiling of {0} is {1}\n", value2, Math.Ceiling(value2)); return 0; } } This would produce: The ceiling of 155.55 is 156 The ceiling of 24.06 is 24 Press any key to continue . . . Besides the Math class, the Double structure provides its own implementation of this method using the following syntax: public static decimal Ceiling(decimal d);
Consider two floating numbers such as 128.44 and 36.72. The number 128.44 is between 128 and 129 with 128 being the lower. The number –36.72 is between –37 and –36 with –37 being the lower. The lowest but closest integer value of a number is referred to as its floor. To assist you with finding the floor of a number, the Math class provides the Floor() method. It is overloaded in two versions whose syntaxes are: public static double Floor(double d); public static decimal Floor(decimal d); The floor() method takes the considered value as the argument and returns the integer that is less than or equal to Value. Here is an example: using System; class Program { static int Main() { double value1 = 1540.25; double value2 = 360.04; Console.WriteLine("The floor of {0} is {1}", value1, Math.Floor(value1)); Console.WriteLine("The floor of {0} is {1}\n", value2, Math.Floor(value2)); return 0; } } This would produce: The floor of 1540.25 is 1540 The floor of 360.04 is 361 Press any key to continue... Instead of using the Math class, the Double structure also has a method to find the floor of a decimal number. Its syntax is: public static decimal Ceiling(decimal d);
The power is the value of one number or expression raised to another number. This follows the formula: ReturnValue = x^{y} To support this operation, the Math class is equipped with a method named Pow whose syntax is public static double Pow(double x, double y); This method takes two arguments. The first argument, x, is used as the base number to be evaluated. The second argument, y, also called the exponent, will raise x to this value. Here is an example: using System; class Program { static int Main() { const double source = 25.38; const double exp = 3.12; double result = Math.Pow(source, exp); Console.WriteLine("Pow({0}, {1}) = {2}\n", source, exp, result); return 0; } } This would produce: Pow(25.38, 3.12) = 24099.8226934415 Press any key to continue . . .
You can calculate the exponential value of a number. To support this, the Math class provides the Exp() method. Its syntax is: public static double Exp(double d); Here is an example of calling this method: using System; class Program { static int Main() { Console.WriteLine("The exponential of {0} is {1}", 709.78222656, Math.Exp(709.78222656)); return 0; } } This would produce: The exponential of 709.78222656 is 1.79681906923757E+308 Press any key to continue . . . If the value of x is less than 708.395996093 (approximately), the result is reset to 0 and qualifies as underflow. If the value of the argument x is greater than 709.78222656 (approximately), the result qualifies as overflow.
To calculate the natural logarithm of a number, you can call the Math.Log() method. It is provides in two versions. The syntax of one is: public static double Log(double d); Here is an example: using System; class Program { static int Main() { double log = 12.48D; Console.WriteLine("Log of {0} is {1}", log, Math.Log(log)); return 0; } } This would produce: Log of 12.48 is 2.52412736294128 Press any key to continue . . .
The Math.Log10() method calculates the base 10 logarithm of a number. The syntax of this method is: public static double Log10(double d); The number to be evaluated is passed as the argument. The method returns the logarithm on base 10 using the formula: y = log_{10}x which is equivalent to x = 10^{y} Here is an example: using System; class Program { static int Main() { double log10 = 12.48D; Console.WriteLine("Log of {0} is {1}", log10, Math.Log10(log10)); return 0; } } This would produce: Log of 12.48 is 1.09621458534641 Press any key to continue . . .
public static double Log(double a, double newBase); The variable whose logarithmic value will be calculated is passed as the first argument to the method. The second argument allows you to specify a base of your choice. The method uses the formula: Y = log_{NewBase}^{x} This is the same as x = NewBase^{y} Here is an example of calling this method: using System; class Program { static int Main() { double logN = 12.48D; Console.WriteLine("Log of {0} is {1}", logN, Math.Log(logN, 4)); return 0; } } This would produce: Log of 12.48 is 1.82077301454376 Press any key to continue . . .
You can calculate the square root of a decimal positive number. To support this, the Math class is equipped with a method named Sqrt whose syntax is: public static double Sqrt(double d); This method takes one argument as a positive floatingpoint number. After the calculation, the method returns the square root of x: using System; class Program { static int Main() { double sqrt = 8025.73D; Console.WriteLine("The square root of {0} is {1}", sqrt, Math.Sqrt(sqrt)); return 0; } } This would produce: The square root of 8025.73 is 89.5864387058666 Press any key to continue . . . 


Previous  Copyright © 2006 FunctionX, Inc.  Next 
