Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Fungsi Matematik dalam C#

Fungsi Matematik dalam C#

王林
王林asal
2024-09-03 15:14:05231semak imbas

Pustaka Matematik dalam C# menyediakan pembangun dengan pelbagai fungsi dan sifat umum, trigonometri, statistik & logaritma dalam matematik. Ini sedia untuk digunakan, pasang dan mainkan perpustakaan. Pustaka mewarisi daripada kelas Objek kelas induk super dalam C#. Ia berada dalam ruang nama Sistem.

Nota: Satu perkara yang sangat penting untuk diingat ialah sifat dan kaedah dalam kelas Matematik semuanya statik, bermakna anda tidak perlu mencipta sebarang objek kelas Matematik untuk memanggilnya.

Sifat Matematik C#

Mari kita lihat pelbagai sifat matematik dalam perpustakaan Matematik.

1. E4

E ialah asas logaritma, yang ditentukan oleh huruf kecil 'e' dalam persamaan matematik. Sifat statik ini memegang nilai asas logaritma semula jadi.

Kod:

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

Output:

Fungsi Matematik dalam C#

2. PI

Pi, yang popular ditulis sebagai simbol p, ialah nisbah lilitan bulatan kepada diameter (kira-kira 3.14). Pemalar statik ini memegang nilai p.

Kod:

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

Output:

Fungsi Matematik dalam C#

Fungsi Matematik C#

Mari kita lihat pelbagai fungsi matematik perpustakaan C# Math yang tersedia untuk kami:

1. Fungsi Mutlak Mutlak

Mengembalikan nilai mutlak nombor tertentu (integer, perpuluhan, titik terapung, dsb.). Nilai mutlak mana-mana nombor ialah nilai perpuluhan maksimum yang mungkin lebih besar daripada atau sama dengan 0 tetapi lebih kecil daripada atau sama dengan nombor itu sendiri.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

2. Pendaraban BigMul-Big

Fungsi ini mengembalikan hasil pendaraban penuh dua nombor integer yang sangat besar. Ia memerlukan dua integer 32 bit dan mengembalikan hasil pendaraban 64-bit.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

3. Lantai & Siling

Fungsi floor() dan siling() mengembalikan nilai lantai dan siling bagi nombor yang ditentukan. Nilai lantai mana-mana nombor ialah integer terbesar yang lebih kecil daripada atau sama dengan nombor itu sendiri. Nilai siling mana-mana nombor ialah integer terkecil yang lebih besar daripada atau sama dengan nombor itu sendiri.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

4. Dosa, Cos & Tan

Fungsi trigonometri ini memberikan nilai sinus, kosinus dan tangen bagi sudut yang ditentukan.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

5. Sinh, Cosh & Tanh–Hiperbola

Fungsi trigonometri ini memberikan nilai sinus hiperbolik, kosinus dan tangen bagi sudut yang ditentukan.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

6. Asin, Acos & Atan

Fungsi trigonometri ini mengembalikan sudut yang nombor yang ditentukan ialah nilai sinus, kosinus atau tangen.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

7. DivRem–Bahagian & Baki

Fungsi ini mengira hasil pembahagian dua integer. Hasilnya tidak dikembalikan dalam nilai pecahan. Sebaliknya, hasil bagi dikembalikan sebagai nilai pulangan fungsi dan selebihnya sebagai parameter output.

Kod:

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);
}
}

Output:

Fungsi Matematik dalam C#

8. Eksponen Eksponen

Fungsi exp mengembalikan e kepada kuasa nombor yang ditentukan.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

9. Log, Log2 & Log10-Logaritma

Fungsi log mengembalikan logaritma nombor tertentu kepada asas yang ditentukan. Jika tiada asas dinyatakan, asas lalai ialah e, ​​menghasilkan logaritma asli.

Nota: Log2 telah diperkenalkan dalam .Net Core. Kaedah ini tidak tersedia dalam .Net Framework.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam C#

10. Min & Maks

Fungsi ini membandingkan dua nombor yang disediakan dan mengembalikan nombor yang lebih kecil atau nombor yang lebih besar daripada kedua-dua nombor itu.

Kod:

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));
}
}

Output:

Fungsi Matematik dalam 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:

Fungsi Matematik dalam 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:

Fungsi Matematik dalam 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:

Fungsi Matematik dalam 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:

Fungsi Matematik dalam 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.

Atas ialah kandungan terperinci Fungsi Matematik dalam C#. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Fungsi Rentetan C#Artikel seterusnya:Fungsi Rentetan C#