Csharp/C Sharp/Development Class/Math
Содержание
Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
using System;
public class Trigonometry {
public static void Main() { Double theta; // angle in radians for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) { Console.WriteLine("Sine of " + theta + " is " + Math.Sin(theta)); Console.WriteLine("Cosine of " + theta + " is " + Math.Cos(theta)); Console.WriteLine("Tangent of " + theta + " is " + Math.Tan(theta)); Console.WriteLine(); } }
}
</source>
Find the radius of a circle given its area.
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Find the radius of a circle given its area.
using System;
public class FindRadius {
public static void Main() { Double r; Double area; area = 10.0; r = Math.Sqrt(area / 3.1416); Console.WriteLine("Radius is " + r); }
}
</source>
Math.Abs
<source lang="csharp"> using System; public class MainClass {
public static void Main() { Console.WriteLine("Math.Abs(5) = {0}", Math.Abs(5)); Console.WriteLine("Math.Abs(-5) = {0}", Math.Abs(-5)); Console.WriteLine("Math.Abs(0) = {0}", Math.Abs(0)); }
}
</source>
Math.Min
<source lang="csharp"> using System; public class MainClass {
public static void Main() { int i = 5; Console.WriteLine("Math.Min(1,2) = {0}", Math.Min(i, 2.5F)); }
}
</source>
Math operators
<source lang="csharp"> using System; class MathOpsApp {
static void Main(string[] args) { Random rand = new Random(); int a, b, c; a = rand.Next() % 100; // Limit max to 99. b = rand.Next() % 100; // Limit max to 99. Console.WriteLine("a={0} b={1}", a, b); c = a * b; Console.WriteLine("a * b = {0}", c); c = a / b; Console.WriteLine("a / b = {0}", c); c = a + b; Console.WriteLine("a + b = {0}", c); c = a - b; Console.WriteLine("a - b = {0}", c); c = a % b; Console.WriteLine("a % b = {0}", c); }
}
</source>
Math.Sign
<source lang="csharp"> using System; public class MainClass {
public static void Main() { Console.WriteLine("Math.Sign(1) = {0}", Math.Sign(1)); Console.WriteLine("Math.Sign(-1) = {0}", Math.Sign(-1)); Console.WriteLine("Math.Sign(0) = {0}", Math.Sign(0)); }
}
</source>
Use Math.Round
<source lang="csharp"> using System; public class MainClass {
public static void Main() { Console.WriteLine(Math.Round(4.4)); Console.WriteLine(Math.Round(4.5)); Console.WriteLine(Math.Round(4.6)); Console.WriteLine(Math.Round(5.5)); Console.WriteLine(Math.Round(4.54, 1)); Console.WriteLine(Math.Round(4.55, 1)); Console.WriteLine(Math.Round(4.65, 1)); Console.WriteLine(Math.Round(4.56, 1)); }
}
</source>