EN
C# / .NET - Math.Asin() method example
0
points
The Math.Asin
function returns number in radians in the range -Math.PI/2
to +Math.PI/2
. The function calculates the inverted sine function value.
using System;
public class Program
{
static double CalculateAngle(double a, double h)
{
return Math.Asin(a / h);
}
public static void Main(string[] args)
{
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b, h;
// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.Sqrt(a * a + b * b);
Console.WriteLine( CalculateAngle(a, h) ); // 0.7853981633974483 <- ~45 degrees
// a, b and h build half of equilateral triangle
a = 3;
b = a * Math.Sqrt(3);
h = Math.Sqrt(a * a + b * b);
Console.WriteLine( CalculateAngle(a, h) ); // 0.5235987755982989 <- ~30 degrees
// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
Console.WriteLine( CalculateAngle(a, h) ); // NaN
}
}
1. Documentation
Syntax |
|
Parameters |
|
Result |
If the value can not be calculated |
Description |
|
2. Working with degrees
using System;
public class Program
{
static double CalculateAngle(double a, double h)
{
double angle = Math.Asin(a / h);
return (180 / Math.PI) * angle; // rad to deg conversion
}
public static void Main(string[] args)
{
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b, h;
// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.Sqrt(a * a + b * b);
Console.WriteLine( CalculateAngle(a, h)); // ~45 degrees
// a, b and h build half of equilateral triangle
a = 3;
b = a * Math.Sqrt(3);
h = Math.Sqrt(a * a + b * b);
Console.WriteLine( CalculateAngle(a, h)); // ~30 degrees
// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
Console.WriteLine( CalculateAngle(a, h)); // NaN
}
}