Home > Net >  Does anyone know why atan2 isn't working?
Does anyone know why atan2 isn't working?

Time:10-11

Does anyone know why this isn't working? I talked to my professor but he told me I needed to use atan2 instead of atan but when I do I just get errors. The project is converting to and from polar and rectangular angles.


//**************************************************************

//**************************************************************
#include <bits/stdc  .h>
#include<iostream>
#include<cmath>
using namespace std;

void RectToPolar(float * a, float * b);
void PolarToRect(float * a, float * b);

//**************************************************************
int main() {

   char ch; // input for program values
   float a, b;

   /* while loop will run until a value 
   of q is reached (both case values) */

   while (1)
   //take in inputs and declare corderanates based off of them 

   {
      cin >> ch >> a >> b; // a = first cordanate b = 2nd

      if (ch == 'Q' || ch == 'q') {
         /*if input is q, stop running program if not continue */
         exit(1);

      }
      //run this step if the input value is r
      else if (ch == 'R' || ch == 'r')

      {

         cout << "REC -> POL                                                  " << endl;

         cout << fixed << setprecision(2) << " REC:  " << "X =  " << a << " Y =  " << b;

         RectToPolar( & a, & b);

         cout << fixed << setprecision(2) << " POL:  " << "M =  " << a << " A =  " << b << endl;

      } else if (ch == 'P' || ch == 'p') { 

         cout << "POL -> REC                                                  " << endl;

         cout << fixed << setprecision(2) << " POL:   " << "M =  " << a << " A =  " << b;

         PolarToRect( & a, & b); //figure out polar to rectagular values of a and b based on input 

         cout << fixed << setprecision(2) << " REC:   " << "X =  " << a << " Y =  " << b;

      }

      /* any input other that is not expected will result in this being the output */
      else {

         cout << "Format Error! ----------------------------------------------" << endl;
      }

   }

   return (0);
}
//**************************************************************

void RectToPolar(float * a, float * b) { //used to convert rectangular to polar

   float m, theta;

   m = sqrt (( * a) * ( * a)   ( * b) * ( * b)); //used to calculate the m value

   theta = atan2 ( * b / * a); //used to calculate the theta value

   * a = m; //used to assign the values to  temp/pointer variables

   * b = theta * (180 / M_PI); //used to convert to degrees because it is in radians
}

//used to convert polar to Rectangular coordinates

void PolarToRect(float * a, float * b) {

   float x, y;

   //convert theta in degree to radians

   * b = ( * b) * (M_PI / 180);

   //calculate the rectangular coordinates

   x = ( * a) * (cos( * b));
   y = ( * a) * (sin( * b));

   //assigning pointer variables
   * a = x;
   * b = y;
}


//**************************************************************



This is the error message I am getting.




designProject2.cpp: In function ‘void RectToPolar(float*, float*)’:
designProject2.cpp:80:29: error: no matching function for call to ‘atan2(float)’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/features.h:461,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/c  config.h:528,
                 from /usr/include/c  /9/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:33,
                 from designProject2.cpp:9:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:59:1: note: candidate: ‘double atan2(double, double)’
   59 | __MATHCALL (atan2,, (_Mdouble_ __y, _Mdouble_ __x));
      | ^~~~~~~~~~
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:59:1: note:   candidate expects 2 arguments, 1 provided
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_Constant, std::_ValArray, _Tp, _Tp>, _Tp> std::atan2(const typename std::valarray<_Tp>::value_type&, const std::valarray<_Tp>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   candidate expects 2 arguments, 1 provided
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_ValArray, std::_Constant, _Tp, _Tp>, _Tp> std::atan2(const std::valarray<_Tp>&, const typename std::valarray<_Tp>::value_type&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   mismatched types ‘const std::valarray<_Tp>’ and ‘float’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_ValArray, std::_ValArray, _Tp, _Tp>, _Tp> std::atan2(const std::valarray<_Tp>&, const std::valarray<_Tp>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   mismatched types ‘const std::valarray<_Tp>’ and ‘float’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename _Dom::value_type> std::atan2(const typename _Dom::value_type&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   candidate expects 2 arguments, 1 provided
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename _Dom::value_type> std::atan2(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   mismatched types ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ and ‘float’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename _Dom::value_type> std::atan2(const std::valarray<typename _Dom::valarray>&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   candidate expects 2 arguments, 1 provided
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename _Dom::value_type> std::atan2(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::valarray<typename _Dom::value_type>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   mismatched types ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ and ‘float’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/c  /9/valarray:603,
                 from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:95,
                 from designProject2.cpp:9:
/usr/include/c  /9/bits/valarray_after.h:548:1: note: candidate: ‘template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::_Atan2, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename _Dom1::value_type> std::atan2(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::_Expr<_Dom2, typename _Dom2::value_type>&)’
  548 | _DEFINE_EXPR_BINARY_FUNCTION(atan2, _Atan2)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /9/bits/valarray_after.h:548:1: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   mismatched types ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ and ‘float’
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:41,
                 from designProject2.cpp:9:
/usr/include/c  /9/cmath:155:5: note: candidate: ‘template<class _Tp, class _Up> constexpr typename __gnu_cxx::__promote_2<_Tp, _Up>::__type std::atan2(_Tp, _Up)’
  155 |     atan2(_Tp __y, _Up __x)
      |     ^~~~~
/usr/include/c  /9/cmath:155:5: note:   template argument deduction/substitution failed:
designProject2.cpp:80:29: note:   candidate expects 2 arguments, 1 provided
   80 |    theta = atan2 ( * b / * a); //used to calculate the theta value
      |                             ^
In file included from /usr/include/x86_64-linux-gnu/c  /9/bits/stdc  .h:41,
                 from designProject2.cpp:9:
/usr/include/c  /9/cmath:148:3: note: candidate: ‘constexpr long double std::atan2(long double, long double)’
  148 |   atan2(long double __y, long double __x)
      |   ^~~~~
/usr/include/c  /9/cmath:148:3: note:   candidate expects 2 arguments, 1 provided
/usr/include/c  /9/cmath:144:3: note: candidate: ‘constexpr float std::atan2(float, float)’
  144 |   atan2(float __y, float __x)
      |   ^~~~~
/usr/include/c  /9/cmath:144:3: note:   candidate expects 2 arguments, 1 provided

When I use normal atan, I dont get an error, but I do get an incorrect result.

When entering

r -40 -30 
p 75 -150
q

The expected result is


REC -> POL                                                  
 REC: X =  -40.00 Y =  -30.00 POL: M =   50.00 A = -143.13
POL -> REC                                                  
 POL: M =   75.00 A = -150.00 REC: X =  -64.95 Y =  -37.50

But the result I am getting is.

REC -> POL                                                  
 REC:  X =  -40.00 Y =  -30.00 POL:  M =  50.00 A =  36.87
POL -> REC                                                  
 POL:   M =  75.00 A =  -150.00 REC:   X =  -64.95 Y =  -37.50

Thanks for any help!

CodePudding user response:

Background and problem:

atan requires a single value for the tangent value.
This value (being a tangent) is the ratio of y / x.

But this value will be the same when e.g. both x and y are positive, and when they are both negative (or when one is positive and the other negative, no matter which is which). But the actual arc tangent should be different.

Solution:

This problem is solved by atan2.
It requires both the y and the x separately and therefore can keep the distinction e.g. between 2 positive and 2 negatives etc.

In order to use it supply it with the 2 arguments, e.g.:

theta = atan2(*b, *a);

CodePudding user response:

The function atan2 takes two parameters: the numerator and the denominator. When calling tan(a / b), you should call tan2(a, b) to consider edge cases.

  •  Tags:  
  • c
  • Related