Languages
[Edit]
EN

C# / .NET - Math.Asin() method example

0 points
Created by:
Dirask Admin - Marvin
1820

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
namespace System
{
    public static class Math
    {
        // ...
        public static double Asin(double number) { ... }
        // ...
    }
}
Parameters

number - double value that represents the result of the operation adjacent / hypotenuse on the right triangle (primitive value).

number should be in the range -1 to +1.

Result

number value in radians in the range -Math.PI/2 to +Math.PI/2 (primitive value).

If the value can not be calculated NaN is returned.

Description

Asin is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arcsine(x).

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
    }
}

References

  1. Inverse trigonometric functions - Wikipedia

C# / .NET - Math object

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join