Csharp/CSharp Tutorial/Development/Math Function

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Calculate the radius of a circle given its area using Math function

<source lang="csharp">using System;

class MainClass {

 public static void Main() {    
   Double r; 
   Double area; 

   area = 10.0; 

   r = Math.Sqrt(area / 3.1416); 

   Console.WriteLine("Radius is " + r); 
 }    

}</source>

Radius is 1.78412203012729

Math.Cos()

<source lang="csharp">using System;

class Example {

 public static void Main() {    
   Double theta; // angle in radians 
    
   for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) { 
     Console.WriteLine("Cosine of " + theta + "  is " + 
                       Math.Cos(theta)); 
     Console.WriteLine(); 
   } 

 }    

}</source>

Cosine of 0.1  is 0.995004165278026
Cosine of 0.2  is 0.980066577841242
Cosine of 0.3  is 0.955336489125606
Cosine of 0.4  is 0.921060994002885
Cosine of 0.5  is 0.877582561890373
Cosine of 0.6  is 0.825335614909678
Cosine of 0.7  is 0.764842187284489
Cosine of 0.8  is 0.696706709347166
Cosine of 0.9  is 0.621609968270665
Cosine of 1  is 0.54030230586814

Math defines several standard mathematical operations.

  1. All of the methods defined by Math are static.
  2. All angles are in radians.

Math also defines these two fields:

  1. public const double E
  2. public const double PI
  3. E is the value of the natural logarithm base, commonly referred to as e.
  4. PI is the value of pi.

The Math class is sealed and it cannot be inherited.

14.13.Math Function 14.13.1. Math defines several standard mathematical operations. 14.13.2. <A href="/Tutorial/CSharp/0280__Development/TheMethodsDefinedbyMath.htm">The Methods Defined by Math </a> 14.13.3. <A href="/Tutorial/CSharp/0280__Development/Mathfunctionsinaction.htm">Math functions in action</a> 14.13.4. <A href="/Tutorial/CSharp/0280__Development/MathSin.htm">Math.Sin()</a> 14.13.5. <A href="/Tutorial/CSharp/0280__Development/CalculatetheradiusofacirclegivenitsareausingMathfunction.htm">Calculate the radius of a circle given its area using Math function</a> 14.13.6. <A href="/Tutorial/CSharp/0280__Development/MathCos.htm">Math.Cos()</a> 14.13.7. <A href="/Tutorial/CSharp/0280__Development/MathTan.htm">Math.Tan()</a>

Math functions in action

<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {

   public static void Main()
   {
       Console.WriteLine("Abs({0}) = {1}", -55, Math.Abs(-55));
       Console.WriteLine("Ceiling({0}) = {1}", 55.3, Math.Ceiling(55.3));
       Console.WriteLine("Pow({0},{1}) = {2}", 10.5, 3, Math.Pow(10.5, 3));
       Console.WriteLine("Round({0},{1}) = {2}",10.55358, 2, Math.Round(10.55358, 2));
       Console.WriteLine("Sin({0}) = {1}", 323.333, Math.Sin(323.333));
       Console.WriteLine("Cos({0}) = {1}", 323.333, Math.Cos(323.333));
       Console.WriteLine("Tan({0}) = {1}", 323.333, Math.Tan(323.333));
   }

}</source>

Abs(-55) = 55
Ceiling(55.3) = 56
Pow(10.5,3) = 1157.625
Round(10.55358,2) = 10.55
Sin(323.333) = 0.248414709883854
Cos(323.333) = -0.968653772982546
Tan(323.333) = -0.256453561440193

Math.Sin()

<source lang="csharp">using System;

class MainClass {

 public static void Main() {    
   Double theta; // angle in radians 
    
   for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) { 
     Console.WriteLine("Sine of " + theta + "  is " + 
                       Math.Sin(theta)); 
     Console.WriteLine(); 
   } 

 }    

}</source>

Sine of 0.1  is 0.0998334166468282
Sine of 0.2  is 0.198669330795061
Sine of 0.3  is 0.29552020666134
Sine of 0.4  is 0.389418342308651
Sine of 0.5  is 0.479425538604203
Sine of 0.6  is 0.564642473395035
Sine of 0.7  is 0.644217687237691
Sine of 0.8  is 0.717356090899523
Sine of 0.9  is 0.783326909627483
Sine of 1  is 0.841470984807896

Math.Tan()

<source lang="csharp">using System;

class Example {

 public static void Main() {    
   Double theta; // angle in radians 
    
   for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) { 
     Console.WriteLine("Tangent of " + theta + "  is " + 
                       Math.Tan(theta)); 
     Console.WriteLine(); 
   } 

 }    

}</source>

Tangent of 0.1  is 0.100334672085451
Tangent of 0.2  is 0.202710035508673
Tangent of 0.3  is 0.309336249609623
Tangent of 0.4  is 0.422793218738162
Tangent of 0.5  is 0.54630248984379
Tangent of 0.6  is 0.684136808341692
Tangent of 0.7  is 0.842288380463079
Tangent of 0.8  is 1.02963855705036
Tangent of 0.9  is 1.26015821755034
Tangent of 1  is 1.5574077246549

The Methods Defined by Math

Method Meaning public static double Abs(double v) the absolute value. public static float Abs(float v) the absolute value. public static decimal Abs(decimal v) the absolute value. public static int Abs(int v) the absolute value. public static short Abs(short v) the absolute value. public static long Abs(long v) the absolute value. public static sbyte Abs(sbyte v) the absolute value. public static double Acos(double v) the arc cosine. v must be between -1 and 1. public static double Asin(double v) the arc sine. v must be between -1 and 1. public static double Atan(double v) the arc tangent. public static double Atan2(double y, double x) the arc tangent of y/x. public static double Ceiling(double v) the smallest integer (represented as a floating-point value) not less than v. For example, given 1.02, Ceiling() returns 2.0. Given -1.02, Ceiling() returns -1. public static double Cos(double v) the cosine. public static double Cosh(double v) the hyperbolic cosine. public static double Exp(double v) the natural logarithm base e raised to the v power. public static double Floor(double v) the largest integer (represented as a floating-point value) not greater than v. For example, given 1.02, Floor() returns 1.0. Given -1.02, Floor() returns -2. public static doubleIEEERemainder(double dividend,double divisor) Returns the remainder of dividend / divisor. public static double Log(double v) the natural logarithm. public static double Log(double v,double base) Returns the logarithm for v using base base. public static double Log10(double v) Returns the base 10 logarithm for v. public static double Max(double v1, double v2) Returns the greater of v1 and v2. public static float Max(float v1, float v2) Returns the greater of v1 and v2. public static decimal Max(decimal v1,decimal v2) Returns the greater of v1 and v2. public static int Max(int v1, int v2) Returns the greater of v1 and v2. public static short Max(short v1, short v2) Returns the greater of v1 and v2. public static long Max(long v1, long v2) Returns the greater of v1 and v2. public static uint Max(uint v1, uint v2) Returns the greater of v1 and v2. public static ushort Max(ushort v1,ushort v2) Returns the greater of v1 and v2. public static ulong Max(ulong v1,ulong v2) Returns the greater of v1 and v2. public static byte Max(byte v1, byte v2) Returns the greater of v1 and v2. public static sbyte Max(sbyte v1, sbyte v2) Returns the greater of v1 and v2. public static double Min(double v1,double v2) Returns the lesser of v1 and v2. public static float Min(float v1, float v2) Returns the lesser of v1 and v2. public static decimal Min(decimal v1,decimal v2) Returns the lesser of v1 and v2. public static int Min(int v1, int v2) Returns the lesser of v1 and v2. public static short Min(short v1, short v2) Returns the lesser of v1 and v2. public static long Min(long v1, long v2) Returns the lesser of v1 and v2. public static uint Min(uint v1, uint v2) Returns the lesser of v1 and v2. public static ushort Min(ushort v1,ushort v2) Returns the lesser of v1 and v2. public static ulong Min(ulong v1, ulong v2) Returns the lesser of v1 and v2. public static byte Min(byte v1, byte v2) Returns the lesser of v1 and v2. public static sbyte Min(sbyte v1, sbyte v2) Returns the lesser of v1 and v2. public static double Pow(double base,double exp) Returns base raised to the exp power(baseexp). public static double Round(double v) Returns v rounded to the nearest whole number. public static decimal Round(decimal v) Returns v rounded to the nearest whole number. public static double Round(double v,int frac) Returns v rounded to the number of fractional digits specified by frac. public static decimal Round(decimal v,int frac) Returns v rounded to the number of fractional digits specified by frac. public static int Sign(double v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(float v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(decimal v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(int v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(short v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(long v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static int Sign(sbyte v) Returns -1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero. public static double Sin(double v) Returns the sine of v. public static double Sinh(double v) Returns the hyperbolic sine of v. public static double Sqrt(double v) Returns the square root of v. public static double Tan(double v) Returns the tangent of v. public static double Tanh(double v) Returns the hyperbolic tangent of v.