有誰知道為什么這不起作用?我和我的教授談過,但他告訴我我需要使用atan2而不是,atan但是當我這樣做時,我只會出錯。該專案正在轉換為極角和矩形角。
//**************************************************************
//**************************************************************
#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;
}
//**************************************************************
這是我收到的錯誤訊息。
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
當我使用普通的 atan 時,我沒有收到錯誤,但我確實得到了不正確的結果。
進入時
r -40 -30
p 75 -150
q
預期的結果是
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
但我得到的結果是。
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
謝謝你的幫助!
uj5u.com熱心網友回復:
背景和問題:
atan切線值需要單個值。
這個值(正切)是 的比率y / x。
x但是當和y都為正時,以及當它們都為負時(或者當一個為正而另一個為負時,無論哪個是哪個),這個值將是相同的。但實際的反正切應該不同。
解決方案:
這個問題解決了atan2。
它分別需要 they和 the x,因此可以保持區分,例如 2 個正數和 2 個負數等。
為了使用它,請為其提供 2 個引數,例如:
theta = atan2(*b, *a);
uj5u.com熱心網友回復:
該函式atan2有兩個引數:分子和分母。呼叫時tan(a / b),您應該呼叫tan2(a, b)以考慮邊緣情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513853.html
標籤:C
