C# 中的數學函數

王林
發布: 2024-09-03 15:14:05
原創
334 人瀏覽過

C# 中的數學函式庫為開發人員提供了各種數學中的通用、三角、統計和對數函數和屬性。這是一個隨時可用、即插即用的函式庫。該函式庫繼承了 C# 中的超級父類別 Object 類別。它駐留在系統命名空間中。

注意: 要記住的一件非常重要的事情是 Math 類別中的屬性和方法都是靜態的,這意味著您不需要創建 Math 類別的任何物件來呼叫它們。

C# 數學屬性

讓我們來看看數學庫中的各種數學屬性。

1. E4

E 是對數底數,在數學方程式中由小寫字母「e」指定。此靜態屬性保存自然對數底的值。

代碼:

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

輸出:

C# 中的數學函數

2. PI

Pi,通常寫成符號 p,是圓的周長與直徑的比值(約 3.14)。這個靜態常數保存 p 的值。

代碼:

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

輸出:

C# 中的數學函數

C# 數學函數

讓我們看看我們可以使用的 C# 數學函式庫的各種數學函數:

1. Abs-絕對函數

傳回給定數字(整數、小數、浮點數等)的絕對值。任何數字的絕對值是大於或等於 0 但小於或等於該數字本身的最大可能小數值。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

2. BigMul-大乘法

此函數傳回兩個非常大的整數的完整乘法結果。它需要兩個 32 位元整數並傳回 64 位元乘法結果。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

3.地板和天花板

floor()和ceiling()函數傳回指定數字的下限和上限值。任何數字的下限值是小於或等於該數字本身的最大整數。任何數字的上限都是大於或等於該數字本身的最小整數。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

4. Sin、Cos 與 Tan

這些三角函數提供指定角度的正弦、餘弦和正切值。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

5. Sinh、Cosh 與 Tanh – 誇張

這些三角函數提供指定角度的雙曲正弦、餘弦和正切值。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

6.阿辛、阿科斯和阿坦

這些三角函數傳回指定數字為正弦、餘弦或正切值的角度。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

7. DivRem – 除法與餘數

此函數計算兩個整數相除的結果。結果不以小數值形式傳回。相反,商數作為函數的返回值返回,餘數作為輸出參數。

代碼:

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);
}
}
登入後複製

輸出:

C# 中的數學函數

8. Exp-指數

exp 函數傳回 e 指定數字的冪。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

9. Log、Log2 與 Log10-對數

log 函數傳回指定數字到指定底數的對數。若未指定底數,則預設底數為 e,得出自然對數。

注意: Log2 是在 .Net Core 中引入的。此方法在 .Net Framework 中不可用。

代碼:

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));
}
}
登入後複製

輸出:

C# 中的數學函數

10。最小和最大

這些函數比較提供的兩個數字並傳回兩個數字中較小的數字或較大的數字。

代碼:

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));
}
}
登入後複製

輸出:

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:

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:

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:

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:

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.

以上是C# 中的數學函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板