Heim  >  Artikel  >  Backend-Entwicklung  >  Mathematische Funktionen in C#

Mathematische Funktionen in C#

王林
王林Original
2024-09-03 15:14:05231Durchsuche

Die Mathematikbibliothek in C# bietet Entwicklern verschiedene allgemeine, trigonometrische, statistische und logarithmische Funktionen und Eigenschaften in der Mathematik. Dies ist eine gebrauchsfertige Plug-and-Play-Bibliothek. Die Bibliothek erbt von der Object-Klasse die übergeordnete übergeordnete Klasse in C#. Es befindet sich im System-Namespace.

Hinweis: Eine sehr wichtige Sache, die Sie im Hinterkopf behalten sollten, ist, dass die Eigenschaften und Methoden in der Math-Klasse alle statisch sind, was bedeutet, dass Sie kein Objekt der Math-Klasse erstellen müssen, um sie aufzurufen.

C#-Mathe-Eigenschaften

Lassen Sie uns einen Blick auf die verschiedenen mathematischen Eigenschaften in der Mathematikbibliothek werfen.

1. E4

E ist die logarithmische Basis, die in mathematischen Gleichungen durch den Kleinbuchstaben „e“ angegeben wird. Diese statische Eigenschaft enthält den Wert der natürlichen logarithmischen Basis.

Code:

using System;
public class Program
{
public static void Main()
{
Console.WriteLine("The value of logarithmic base E is " + Math.E);
}
}

Ausgabe:

Mathematische Funktionen in C#

2. PI

Pi, im Volksmund als Symbol p geschrieben, ist das Verhältnis des Kreisumfangs zum Durchmesser (ungefähr 3,14). Diese statische Konstante enthält den Wert von p.

Code:

using System;
public class Program
{
public static void Main()
{
Console.WriteLine("The value of PI is " + Math.PI);
}
}

Ausgabe:

Mathematische Funktionen in C#

C#-Mathefunktionen

Lassen Sie uns einen Blick auf die verschiedenen mathematischen Funktionen der C#-Mathe-Bibliothek werfen, die uns zur Verfügung stehen:

1. Abs-Absolutfunktion

Gibt den absoluten Wert einer bestimmten Zahl zurück (Ganzzahl, Dezimalzahl, Gleitkommazahl usw.). Der Absolutwert einer beliebigen Zahl ist der maximal mögliche Dezimalwert, der größer oder gleich 0, aber kleiner oder gleich der Zahl selbst ist.

Code:

using System;
public class Program
{
public static void Main()
{
int num1 = 231;
double num2 = -1.23456789;
Console.WriteLine("The absolute value of {0} is {1} ", num1,  Math.Abs(num1));
Console.WriteLine("The absolute value of {0} is {1} ", num2,  Math.Abs(num2));
}
}

Ausgabe:

Mathematische Funktionen in C#

2. BigMul-Big Multiplikation

Diese Funktion gibt das vollständige Multiplikationsergebnis zweier sehr großer Ganzzahlen zurück. Es nimmt zwei 32-Bit-Ganzzahlen und gibt ein 64-Bit-Multiplikationsergebnis zurück.

Code:

using System;
public class Program
{
public static void Main()
{
int num1 = Int32.MaxValue;
Console.WriteLine("Multiplication of {0}x{0} without Math function - {1}",num1, num1*num1);
Console.WriteLine("Multiplication of {0}x{0} by Math BigMul function - {1}",num1, Math.BigMul(num1, num1));
}
}

Ausgabe:

Mathematische Funktionen in C#

3. Boden & Decke

Die Funktionen floor() und ceiling() geben die Boden- und Deckenwerte einer angegebenen Zahl zurück. Der Mindestwert einer Zahl ist die größte ganze Zahl, die kleiner oder gleich der Zahl selbst ist. Der Höchstwert einer Zahl ist die kleinste ganze Zahl, die größer oder gleich der Zahl selbst ist.

Code:

using System;
public class Program
{
public static void Main()
{
double num1 = 548.65;
Console.WriteLine("Floor value of {0} is {1}", num1, Math.Floor(num1));
Console.WriteLine("Ceil value of {0} is {1}", num1, Math.Ceiling(num1));
}
}

Ausgabe:

Mathematische Funktionen in C#

4. Sin, Cos & Tan

Diese trigonometrischen Funktionen liefern den Sinus-, Kosinus- und Tangenswert des angegebenen Winkels.

Code:

using System;
public class Program
{
public static void Main()
{
double angle = 120.5;
Console.WriteLine("Sine value of {0} is {1}", angle, Math.Sin(angle));
Console.WriteLine("Cosine value of {0} is {1}", angle,Math.Cos(angle));
Console.WriteLine("Tangent value of {0} is {1}", angle, Math.Tan(angle));
}
}

Ausgabe:

Mathematische Funktionen in C#

5. Sinh, Cosh & Tanh–Hyperbel

Diese trigonometrischen Funktionen liefern den hyperbolischen Sinus-, Kosinus- und Tangenswert des angegebenen Winkels.

Code:

using System;
public class Program
{
public static void Main()
{
double angle = 120.5;
Console.WriteLine("Hyperbolic Sine value of {0} is {1}", angle, Math.Sinh(angle));
Console.WriteLine("Hyperbolic Cosine value of {0} is {1}", angle, Math.Cosh(angle));
Console.WriteLine("Hyperbolic Tangent value of {0} is {1}", angle,Math.Tanh(angle));
}
}

Ausgabe:

Mathematische Funktionen in C#

6. Asin, Acos & Atan

Diese trigonometrischen Funktionen geben den Winkel zurück, zu dem die angegebene Zahl der Sinus-, Cosinus- oder Tangenswert ist.

Code:

using System;
public class Program
{
public static void Main()
{
double value = 1;
Console.WriteLine("The angle of sin({0}) is {1}", value, Math.Asin(value));
Console.WriteLine("The angle of cos({0}) is {1}", value, Math.Acos(value));
Console.WriteLine("The angle of tan({0}) is {1}", value, Math.Atan(value));
}
}

Ausgabe:

Mathematische Funktionen in C#

7. DivRem–Division & Rest

Diese Funktion berechnet das Ergebnis einer Division zweier Ganzzahlen. Das Ergebnis wird nicht als Bruchwert zurückgegeben. Vielmehr wird der Quotient als Rückgabewert der Funktion und der Rest als Ausgabeparameter zurückgegeben.

Code:

using System;
public class Program
{
public static void Main()
{
int divisor = 8;
int dividend = 45;
int remainder = 0;
int quotient = Math.DivRem(dividend, divisor, out remainder);
Console.WriteLine("{0} divided by {1} results in {2} as the quotient and {3} as the remainder.", dividend, divisor, quotient, remainder);
}
}

Ausgabe:

Mathematische Funktionen in C#

8. Exp-Exponentiell

Die exp-Funktion gibt e zur Potenz der angegebenen Zahl zurück.

Code:

using System;
public class Program
{
public static void Main()
{
int power = 4;
Console.WriteLine("{0} to the power of {1} is {2}.", Math.E, power, Math.Exp(power));
}
}

Ausgabe:

Mathematische Funktionen in C#

9. Log, Log2 und Log10-Logarithmus

Die Protokollfunktion gibt den Logarithmus einer angegebenen Zahl zu einer angegebenen Basis zurück. Wenn keine Basis angegeben ist, ist die Standardbasis e, was zum natürlichen Logarithmus führt.

Hinweis: Log2 wurde in .Net Core eingeführt. Diese Methode ist im .Net Framework nicht verfügbar.

Code:

using System;
public class Program
{
public static void Main()
{
double num1 = 4.5;
int new_base = 12;
Console.WriteLine("Log({0}) to the base 'e' is {1}.", num1, Math.Log(num1));
Console.WriteLine("Log({0}) to the base 10 is {1}.", num1,Math.Log10(num1));
Console.WriteLine("Log({0}) to the base 2 is {1}.", num1,Math.Log(num1, 2));
Console.WriteLine("Log({0}) to the base {1} is {2}.", num1,new_base, Math.Log(num1, new_base));
}
}

Ausgabe:

Mathematische Funktionen in C#

10. Min. und Max.

Diese Funktionen vergleichen die beiden bereitgestellten Zahlen und geben die kleinere oder die größere Zahl der beiden zurück.

Code:

using System;
public class Program
{
public static void Main()
{
double num1 = 4.5;
double num2 = -3.4;
int num3 = 981;
int num4 = 123;
Console.WriteLine("Minimum of {0} and {1} is {2}.", num1, num2,Math.Min(num1, num2));
Console.WriteLine("Maximum of {0} and {1} is {2}.", num1, num2,Math.Max(num1, num2));
Console.WriteLine("Minimum of {0} and {1} is {2}.", num3, num4,Math.Min(num3, num4));
Console.WriteLine("Maximum of {0} and {1} is {2}.", num3, num4,Math.Max(num3, num4));
}
}

Ausgabe:

Mathematische Funktionen in C#

11. Pow-Power

The pow() function returns the specified number to the specified power.

Code:

using System;
public class Program
{
public static void Main()
{
int num1 = 11;
double num2 = 3.4;
Console.WriteLine("{0} to the power {1} is {2}.", num1, num2, Math.Pow(num1, num2));
Console.WriteLine("The cube of {0} is {1}.", num1, Math.Pow(num1, 3));
}
}

Output:

Mathematische Funktionen in C#

12. Round

The round() function, as the name suggests, rounds the specified number to the nearest integer or specified decimal places after the integer.

There are a few important variations of round() function. It takes either two or three arguments.

  1. The first argument is the number to be rounded.
  2. The second argument is the number of digits after the decimal point. If this is not specified, the number is rounded to the nearest integer.
  3. The third argument is the mode of rounding. This is an enumeration of two values derived accessed from the enum MidpointRounding.

The two modes are:

  • AwayFromZero: When a number falls halfway between two numbers, it is rounded to the nearest number which is farther from zero.
  • ToEven: When a number falls halfway between two numbers, it is rounded to the nearest even number.

If not specified, the mode AwayFromZero is the default mode.

Code:

using System;
public class Program
{
public static void Main()
{
double num1 = 2.45;
double num2 = 24.5;
Console.WriteLine("{0} rounded to the nearest integer is {1}", num1, Math.Round(num1));
Console.WriteLine("{0} rounded to the nearest single-point decimal is {1}", num1, Math.Round(num1, 1));
Console.WriteLine("{0} rounded to the nearest single-point decimal away from zero is {1}", num1, Math.Round(num1, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0} rounded to the nearest single-point decimal to even is {1}", num1, Math.Round(num1, 1, MidpointRounding.ToEven));
Console.WriteLine("\n{0} rounded to the nearest integer away from zero is {1}", num2, Math.Round(num2, MidpointRounding.AwayFromZero));
Console.WriteLine("{0} rounded to the nearest integer to even is {1}", num2, Math.Round(num2, MidpointRounding.ToEven));
}
}

Output:

Mathematische Funktionen in C#

13. Sqrt-Square Root

This function returns the square root of the given number.

Code:

using System;
public class Program
{
public static void Main()
{
int num1 = 196;
double num2 = 404.1;
Console.WriteLine("Square root of {0} is {1}.", num1,Math.Sqrt(num1));
Console.WriteLine("Square root of {0} is {1}.", num2, Math.Sqrt(num2));
}
}

Output:

Mathematische Funktionen in C#

14. Truncate

The truncate function returns an integral part of the specified number. So, in simple terms, it discards anything after the decimal point and returns everything before the decimal point.

Note: Note that this is different from Round function. The round function returns an integer nearest to the number. It may be an integer greater than the number itself. Whereas, Truncate function would always return the integer part of the number as is. E.g. – Round(4.9) results in 5. Truncate(4.9) results in 4.

Code:

using System;
public class Program
{
public static void Main()
{
double num1 = 404.92;
Console.WriteLine("Truncated value of {0} is {1}.", num1, Math.Truncate(num1));
Console.WriteLine("Rounded-off value of {0} is {1}.", num1, Math.Round(num1));
}
}

Output:

Mathematische Funktionen in C#

Conclusion

This article covered almost all the mathematical functions provided in the C# Math library. This library proves to be very useful due to the plug-n-play mathematical properties and functions, thereby making development easier.

Das obige ist der detaillierte Inhalt vonMathematische Funktionen in C#. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:C#-String-FunktionenNächster Artikel:C#-String-Funktionen