Академический Документы
Профессиональный Документы
Культура Документы
htm
If you consider it in the purest sense of a computer language like C, C++, Pascal,
Visual Basic, and Java, etc, C# doesn't have its own built-in support for mathematics.
It must borrow this functionality either from other libraries or from other language.
Fortunately, all of this is particularly easy.
To perform the basic algebraic and geometric operations in C#, you can use methods
of the Math class of the .NET Framework. As seen in the previous lesson, you can
also take advantage of Visual Basic's very powerful library of functions. This library is
one of the most extended set of functions of various area of business mathematics.
In Lesson 1, we saw different ways to declare a variable of a numeric type. Here are
examples:
using System;
class Program
{
static int Main()
{
short sNumber;
int iNumber;
double dNumber;
decimal mNumber;
return 0;
}
}
One of the primary rules to observe in C# is that, after declaring a variable, before
using it, it must have been initialized. Here are examples of initializing variables:
using System;
class Program
{
static int Main()
{
short sNumber = 225;
int iNumber = -847779;
double dNumber = 9710.275D;
decimal mNumber = 35292742.884295M;
return 0;
}
}
1 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
To find out about the sign of a value or a numeric variable, you can call the
Math.Sign() method. It is overloaded in various versions whose syntaxes are:
public static int Sign(sbyte value);
public static int Sign(short value);
public static int Sign(int value);
public static int Sign(long value);
public static int Sign(sbyte value);
public static int Sign(double value);
public static int Sign(decimal value);
When calling this method, pass the value or the variable you want to consider, as
argument. The method returns:
class Program
{
static int Main()
{
short sNumber = 225;
int iNumber = -847779;
double dNumber = 9710.275D;
decimal mNumber = 35292742.884295M;
return 0;
}
}
2 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
To get the integral part of a decimal number, the Math class can assist you with the
Trancate() method, which is overloaded in two versions whose syntaxes are:
public static double Truncate(double d);
public static double Truncate(double d);
When calling this method, pass it a number or a variable of float, double, or decimal
type. The method returns the int side of the value. Here is an example of calling it:
using System;
class Program
{
static int Main()
{
float number = 225.75f;
return 0;
}
}
If you have two numbers, you can find the minimum of both without writing your
own code. To assist you with this, the Math class is equipped with a method named
Min. This method is overloaded in various versions with each version adapted to each
integral or floating-point data type. The syntaxes are:
public static byte Min(byte val1, byte val2);
public static sbyte Min(sbyte val1, sbyte val2);
public static short Min(short val1, short val2);
public static ushort Min(ushort val1, ushort val2);
public static int Min(int val1, int val2);
public static uint Min(uint val1, uint val2);
public static float Min(float val1, float val2);
public static long Min(long val1, long val2);
public static ulong Min(ulong val1, ulong val2);
public static double Min(double val1, double val2);
public static decimal Min(decimal val1, decimal val2);
3 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
using System;
class Program
{
static int Main()
{
int number1 = 8025;
int number2 = 73;
return 0;
}
}
As opposed to the minimum of two numbers, you may be interested in the higher of
both. To help you find the maximum of two numbers, you can call the Max() method
of the Math class. It is overloaded in various versions with one of each type of
numeric data. The syntaxes of this method are:
public static byte Max(byte val1, byte val2);
public static sbyte Max(sbyte val1, sbyte val2);
public static short Max(short val1, short val2);
public static ushort Max(ushort val1, ushort val2);
public static int Max(int val1, int val2);
public static uint Max(uint val1, uint val2);
public static float Max(float val1, float val2);
public static long Max(long val1, long val2);
public static ulong Max(ulong val1, ulong val2);
public static double Max(double val1, double val2);
public static decimal Max(decimal val1, decimal val2);
class Program
{
static int Main()
{
int number1 = 8025;
int number2 = 73;
return 0;
}
}
4 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
As you can see, a value held by a Byte variable can fit in the memory reserved for an
int variable, which can be carried by a long variable. Thanks to this, you can assign a
Byte value to an int variable, or an int variable to a long variable. Also, based on this,
because the memory reserved for an int variable is larger than the one reserved for a
double variable, you can assign a variable of the former to a variable of the latter.
Here is an example:
using System;
class Program
{
static int Main()
{
int iNumber = 2445;
double dNumber = iNumber;
5 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
possible. Since the memory reserved for a short variable is smaller than that of an
int, you cannot assign the value of an int to a short variable. Consider the following
program:
using System;
class Program
{
static int Main()
{
int iNumber = 168;
short sNumber = iNumber;
Value casting consists of converting a value of one type into a value of another type.
For example, you may have an integer value and you may want that value in an
expression that expects a short. Value casing is also referred to as explicit conversion.
To cast a value or a variable, precede it with the desired data type in parentheses.
Here is an example:
using System;
class Program
{
static int Main()
{
int iNumber = 168;
short sNumber = (short)iNumber;
When performing explicit conversion, you should pay close attention to the value that
is being cast. If you want an integer value to be assigned to a short variable, the value
must fit in 16 bits, which means it must be between -32768 and 32767. Any value
beyond this range would proceed an unpredictable result. Consider the following
program:
using System;
class Program
6 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
{
static int Main()
{
int iNumber = 680044;
short sNumber = (short)iNumber;
In Lesson 6 and Lesson 17, we saw that each C# data type, which is adapted from a
.NET Framework structure, was equipped with a ToString() method that could be
used to convert its value to a String type. We didn't address the possibility of
converting a value from one primitive type to another. To support the conversion of a
value from one type to another, the .NET Framework provides a class named
Convert. This class is equipped with various static methods; they are so numerous
that we cannot review all of them.
Remember that each primitive data type of the C# language is type-defined from a
.NET Framework structure as follows:
.NET Framework
C# Data Type Name
Structure
bool Bollean Boolean
byte Byte Byte
sbyte Signed Byte SByte
char Character Char
short Small Integer Int16
ushort Unsigned Small Integer UInt16
int Integer Int32
uint Unsigned Integer UInt32
long Long Integer Int64
ulong Unsigned Long Integer UInt64
float Single-Precision Floating-Point Single
double Double-Precision Floating-Point Double
Extended Precision Floating-Point
decimal Decimal
Number
No Explicit
Date/Time Value DateTime
Type
string String String
To adapt the Convert class to each C# data type, the class is equipped with a static
7 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
method whose name starts with To, ends with the .NET Framework name of its
structure, and takes as argument the type that needs to be converted. Based on this,
to convert a decimal number of a double type to a number of int type, you can call
the ToInt32() method and pass the double variable as argument. Its syntax is:
public static int ToInt32(double value);
Here is an example:
using System;
class Program
{
static int Main()
{
double dNumber = 34987.68D;
int iNumber = Convert.ToInt32(dNumber);
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;
8 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
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);
class Program
{
static int Main()
{
double value1 = 155.55; double value2 = -24.06;
Besides the Math class, the Double structure provides its own implementation of
this method using the following syntax:
public static decimal Ceiling(decimal d);
9 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
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;
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 = xy
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
10 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
{
static int Main()
{
const double source = 25.38;
const double exp = 3.12;
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);
class Program
{
static int Main()
{
Console.WriteLine("The exponential of {0} is {1}",
709.78222656, Math.Exp(709.78222656));
return 0;
}
}
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()
{
11 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
return 0;
}
}
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 = log10x
which is equivalent to
x = 10y
Here is an example:
using System;
class Program
{
static int Main()
{
double log10 = 12.48D;
return 0;
}
}
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 = logNewBasex
12 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
x = NewBasey
class Program
{
static int Main()
{
double logN = 12.48D;
return 0;
}
}
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 floating-point number. After the
calculation, the method returns the square root of x:
using System;
class Program
{
static int Main()
{
double sqrt = 8025.73D;
return 0;
}
}
13 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
A circle is a group or series of distinct points drawn at an exact same distance from
another point referred to as the center. The distance from the center C to one of these
equidistant points is called the radius, R. The line that connects all of the points that
are equidistant to the center is called the circumference of the circle. The diameter is
the distance between two points of the circumference to the center; in other words, a
diameter is double the radius.
The distance between two equidistant points A and B is a round shape geometrically
defined as an arc. An angle, ө, is the ratio of the distance between two points A and B
of the circumference divided by the radius R. This can be written as:
Therefore, an angle is the ratio of an arc over the radius. Because an angle is a ratio
and not a “physical” measurement, which means an angle is not a dimension, it is
independent of the size of a circle. Obviously this angle represents the number of
portions included by the three points. A better unit used to measure an angle is the
radian or rad.
A cycle is a measurement of the rotation around the circle. Since the rotation is not
necessarily complete, depending on the scenario, a measure is made based on the
angle that was covered during the rotation. A cycle could cover part of the circle in
which case the rotation would not have been completed. A cycle could also cover the
whole 360˚ of the circle and continue there after. A cycle is equivalent to the radian
divided by 2 * Pi.
The word п, also written as Pi, is a constant number used in various mathematical
calculations. Its approximate value is 3.1415926535897932. The calculator of
Windows represents it as 3.1415926535897932384626433832795. Borland had
included its value in the math.h library as M_PI 3.14159265358979323846.
14 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
2 * R * Pi.
A radian is 2Rп/R radians or 2Rп/R rad, which is the same as 2п rad or 2 * Pi rad.
To perform conversions between the degree and the radian, you can use the formula:
To calculate the cosine of an angle, the Math class provides the Cos() method. Its
syntax is:
public static double Cos(double d);
Here is an example:
using System;
class Program
{
static int Main()
{
int number = 82;
return 0;
}
}
Consider AB the length of A to B, also called the hypotenuse to point A. Also consider
CB the length of C to B, which is the opposite side to point A. The sine represents the
ratio of CB/AB; that is, the ratio of the opposite side, CB over the hypotenuse AB.
15 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
To calculate the sine of a value, you can call the Sin() method of the Math class. Its
syntax is:
public static double Sin(double a);
Here is an example:
using System;
class Program
{
static int Main()
{
double number = 82.55;
return 0;
}
}
Here is an example:
using System;
class Program
{
static int Main()
{
uint number = 225;
return 0;
}
}
Here is an example:
16 de 17 5/10/2018 11:43 a. m.
Lesson 00: Mathematics in C# http://www.functionx.com/csharp3/math/Lesson20.htm
using System;
class Program
{
static int Main()
{
short number = 225;
return 0;
}
}
17 de 17 5/10/2018 11:43 a. m.