none
Incorrect figures with Atan2 and Sqrt RRS feed

  • Question

  • Hi,

    I am having a problem woth Atan and Sqrt formulas. My Excel gives me a figure where my C# gives a different figure although it's the same formula.

    The forumala in Excel is:

    =2*ATAN2(SQRT(1-B15),SQRT(B15))

    and in C#:

    double the_c = Math.Atan2(Math.Sqrt(1 - the_a), Math.Sqrt(the_a));


    Here is the Excelsheet:

    https://www.jassimrahma.net/temp/DistanceAndBearingToWaypoint.zip

    Here is my code and the problem is on the_c line

    current_latitude = 25.2680572;
    current_longitude = 55.3159565;
    
    double latt_from_radians = current_latitude * Math.PI / 180;
    double long_from_radians = current_longitude * Math.PI / 180;
    double latt_to_radians = HolyPlaceLatitude * Math.PI / 180;
    double lang_to_radians = HolyPlaceLongitude * Math.PI / 180;
    
    int the_r = 6371;            // for miles
                                // int r = 6371;            // for kilometers
                                // int r = 20, 903, 520;    // to get the result in feet
    
    double law_of_cos_dist = Math.Acos((Math.Cos(latt_from_radians) * Math.Cos(latt_to_radians) * Math.Cos((-1 * lang_to_radians) - (-1 * long_from_radians))) + (Math.Sin(latt_from_radians) * Math.Sin(latt_to_radians))) * the_r;
    
    LabelA.Text = law_of_cos_dist.ToString();
    
    double dLat = latt_to_radians - latt_from_radians;
    double dLong = lang_to_radians - long_from_radians;
    
    double the_a = Math.Pow((Math.Sin(dLat / 2)), 2) + Math.Cos(latt_from_radians) * Math.Cos(latt_to_radians) * Math.Pow((Math.Sin(dLong / 2)), 2);
    
    LabelB.Text = the_a.ToString();
    
    double the_c = Math.Atan2(Math.Sqrt(1 - the_a), Math.Sqrt(the_a));
    LabelC.Text = the_c.ToString();
    

    Kindly help...

    Thanks,

    Jassim

    Sunday, August 18, 2019 12:39 AM

Answers

  • I found the issue

    In Excel it is Atan2(x, y) but it should be reversed in C# to Atan2(y, x)

    Thanks,

    Jassim

    • Marked as answer by Jassim Rahma Sunday, August 18, 2019 12:50 AM
    Sunday, August 18, 2019 12:50 AM