Csharp/C Sharp by API/System/Math
Содержание
Math.Abs
using System;
public class MainClass {
public static void Main( ) {
double epsilon = 1.0e-9;
double guess = 11.0;
double result = 0.0;
double value = 2;
result = ((value / guess) + guess) / 2;
do {
Console.WriteLine( "Guess Value = {0}", guess );
Console.WriteLine( "Result Value = {0}", result );
guess = result;
result = ((value / guess) + guess) / 2;
} while( Math.Abs(result - guess) > epsilon );
Console.WriteLine("The approx sqrt of {0} is {1}", value, result );
}
}
Math.Cos()
using System;
public class Trigonometry {
public static void Main() {
Double theta;
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();
}
}
}
Math.Min
using System;
public class MainClass {
public static void Main() {
int i = 5;
Console.WriteLine("Math.Min(1,2) = {0}", Math.Min(i, 2.5F));
}
}
Math.PI
using System;
using System.Data;
class Class1{
static void Main(string[] args){
Console.WriteLine(ConvertDegreesToRadians (180));
Console.WriteLine(ConvertDegreesToRadians (90));
Console.WriteLine(ConvertDegreesToRadians (30));
}
public static double ConvertDegreesToRadians (double degrees)
{
double radians = (Math.PI / 180) * degrees;
return (radians);
}
}
Math.Round
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));
}
}
Math.Sign
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));
}
}
Math.Sin()
using System;
public class Trigonometry {
public static void Main() {
Double theta;
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();
}
}
}
Math.Sqrt
using System;
class MainClass {
public static void Main() {
double s1 = 3.0;
double s2 = 4.0;
double hypot;
hypot = Math.Sqrt(s1*s1 + s2*s2);
Console.WriteLine("Hypotenuse is " + hypot);
}
}
Math.Tan()
using System;
public class Trigonometry {
public static void Main() {
Double theta;
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();
}
}
}