溫馨提示×

c# math類代碼如何編寫

c#
小樊
81
2024-10-18 12:11:23
欄目: 編程語言

在C#中,可以使用System.Math類來執(zhí)行各種數(shù)學運算。下面是一些示例代碼:

  1. 計算兩個數(shù)的和:
double num1 = 10.5;
double num2 = 5.5;
double sum = Math.Add(num1, num2);
Console.WriteLine("The sum of " + num1 + " and " + num2 + " is " + sum);
  1. 計算一個數(shù)的平方:
double num = 4;
double square = Math.Pow(num, 2);
Console.WriteLine("The square of " + num + " is " + square);
  1. 計算一個數(shù)的正弦值:
double num = Math.PI;
double sine = Math.Sin(num);
Console.WriteLine("The sine of " + num + " is " + sine);
  1. 計算一個數(shù)的余弦值:
double num = Math.PI;
double cosine = Math.Cos(num);
Console.WriteLine("The cosine of " + num + " is " + cosine);

這些示例代碼演示了如何使用System.Math類中的靜態(tài)方法執(zhí)行基本的數(shù)學運算。你可以根據(jù)需要修改這些示例代碼,以適應你的具體需求。

0