/*****************************************************************
Name : 
Date : 2018/02/07
By   : CharlotteHonG
Final: 2018/02/07
*****************************************************************/
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define M_PI 3.14159265358979323846

// 快速 atan2 算法
float fastAtan2f(float dy, float dx){
    static const float atan2_p1 = 0.9997878412794807f*(float)(180/M_PI);
    static const float atan2_p3 = -0.3258083974640975f*(float)(180/M_PI);
    static const float atan2_p5 = 0.1555786518463281f*(float)(180/M_PI);
    static const float atan2_p7 = -0.04432655554792128f*(float)(180/M_PI);
    static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;

    float ax = std::abs(dx), ay = std::abs(dy);
    float a, c, c2;
    if (ax >= ay) {
        c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    } else {
        c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }
    if (dx < 0)
        a = 180.f - a;
    if (dy < 0)
        a = 360.f - a;
    return a;
}
float fastAtan2f_rad(float dy, float dx){
    static const float atan2_p1 =  0.9997878412794807f;
    static const float atan2_p3 = -0.3258083974640975f;
    static const float atan2_p5 =  0.1555786518463281f;
    static const float atan2_p7 = -0.04432655554792128f;
    static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;

    float ax = std::abs(dx), ay = std::abs(dy);
    float a, c, c2;
    if (ax >= ay) {
        c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    } else {
        c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = M_PI/0.5 - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }
    if (dx < 0)
        a = M_PI - a;
    if (dy < 0)
        a = M_PI*2.0 - a;
    return a;
}
// 快速 atan 算法
float fastAtanf(float dy){
    static const float atan2_p1 = 0.9997878412794807f*(float)(180/M_PI);
    static const float atan2_p3 = -0.3258083974640975f*(float)(180/M_PI);
    static const float atan2_p5 = 0.1555786518463281f*(float)(180/M_PI);
    static const float atan2_p7 = -0.04432655554792128f*(float)(180/M_PI);
    static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;

    float ax = 1.0, ay = std::abs(dy);
    float a, c, c2;
    if (ax >= ay) {
        c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    } else {
        c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }

    if (dy < 0)
        a = - a;

    return a;
}
float fastAtanf_rad(float dy){
    static const float atan2_p1 =  0.9997878412794807f;
    static const float atan2_p3 = -0.3258083974640975f;
    static const float atan2_p5 =  0.1555786518463281f;
    static const float atan2_p7 = -0.04432655554792128f;
    static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;

    float ax = 1.0, ay = std::abs(dy);
    float a, c, c2;
    if (ax >= ay) {
        c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    } else {
        c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
        c2 = c*c;
        a = M_PI*0.5 - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }

    if (dy < 0)
        a = - a;

    return a;
}

double rounding(double num, int index)
{
    bool isNegative = false; // whether is negative number or not
    
    if(num < 0) // if this number is negative, then convert to positive number
    {
        isNegative = true;  
        num = -num;
    }
    
    if(index >= 0)
    {
        int multiplier;
        multiplier = pow(10, index);
        num = (int)(num * multiplier + 0.5) / (multiplier * 1.0);
    }
    
    if(isNegative) // if this number is negative, then convert to negative number
    {
        num = -num;
    }
    
    return num;
}

void test_fast(int i) {
    
    int corn = i;
    float val = corn *M_PI/180.0;
    float test = tan(val);

    float theta2 = atan(test) *180.0/M_PI;
    float theta3 = fastAtanf_rad(test) *180.0/M_PI;

    int idx = 2;
    if( rounding(theta2, idx) != rounding(theta3, idx)) {
        cout << fixed << setprecision(2) << "atan- cmath/fast = (" << theta2 << ", " << theta3 << ")" << endl;
    }
    //cout << fixed << setprecision(2) << "atan- cmath/fast = (" << theta2 << ", " << theta3 << ")" << endl;
}
//================================================================
int main(int argc, char const *argv[]){

    for(int i = -720; i < 720; ++i) {
        test_fast(i);
    }
    return 0;
}
//====================================================================================
