>  기사  >  백엔드 개발  >  C#의 수학 함수

C#의 수학 함수

王林
王林원래의
2024-09-03 15:14:05231검색

C#의 수학 라이브러리는 개발자에게 수학의 다양한 일반, 삼각, 통계, 로그 함수와 속성을 제공합니다. 플러그 앤 플레이 라이브러리를 사용할 준비가 되었습니다. 라이브러리는 C#의 상위 상위 클래스인 Object 클래스에서 상속됩니다. 이는 System 네임스페이스에 있습니다.

참고: 유념해야 할 매우 중요한 점 중 하나는 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. 삐

일반적으로 기호 p로 표기되는 Pi는 원주와 지름의 비율(약 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. 절대-절대 함수

주어진 숫자(정수, 소수, 부동 소수점 등)의 절대값을 반환합니다. 숫자의 절대값은 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() 및 천장() 함수는 지정된 숫자의 바닥 및 천장 값을 반환합니다. 숫자의 하한값은 숫자 자체보다 작거나 같은 가장 큰 정수입니다. 숫자의 상한값은 숫자 자체보다 크거나 같은 가장 작은 정수입니다.

코드:

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. 신, 코스 & 탄

이러한 삼각 함수는 지정된 각도의 사인, 코사인 및 탄젠트 값을 제공합니다.

코드:

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 함수는 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 문자열 함수다음 기사:C# 문자열 함수