Функция valarray atan2 () в C ++
Функция atan2 () определена в заголовочном файле valarray. Эта функция вычисляет арктангенс значения (y / x) каждого элемента в valarray и возвращает valarray, содержащий арктангенс всех элементов. где y - доля координаты y, а x - доля координаты x.
Синтаксис:
std :: valarray res = atan2 (координаты y, координаты x)
Параметры: функция принимает два обязательных параметра: координаты X и координаты Y.
Примечание. Если оба параметра являются объектами valarray и их размеры не совпадают, они ведут себя как неопределенные.
Returns: эта функция возвращает valarray, содержащий арктангенс всех элементов.
Нижеприведенные программы иллюстрируют вышеуказанную функцию:
Пример 1: -
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // intilaize both the array X and Y coords double y[] = { 0.0, 3.0, -2.0 }; double x[] = { -3.0, 3.0, -1.0 }; // intilaize both the valarray X and Y coords valarray< double > ycoords(y, 3); valarray< double > xcoords(x, 3); // store results in valarray res valarray< double > res = atan2 (ycoords, xcoords); // print results of atan2() function cout << "results:" ; for ( size_t i = 0; i < res.size(); ++i) cout << ' ' << res[i]; cout << '
' ; return 0; } |
Выход:
результаты: результаты: 3.14159 0.785398 -2.03444
Пример 2: -
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // intilaize both the array X and Y coords double y[] = { 4.0, 5.6, -2.8, 7.3 }; double x[] = { 5.0, -1.5, 7.0, -0.8 }; // intilaize both the valarray X and Y coords valarray< double > ycoords(y, 4); valarray< double > xcoords(x, 4); // store results in valarray res valarray< double > res = atan2 (ycoords, xcoords); // print results of atan2() function cout << "results:" ; for ( size_t i = 0; i < res.size(); ++i) cout << ' ' << res[i]; cout << '
' ; return 0; } |
Выход:
результаты: 0,674741 1,83251 -0,380506 1,67995
Пример 3: - Ошибки и исключения: функция не возвращает подходящую функцию для вызова ошибки, когда в качестве аргумента передаются объекты valarray разных размеров.
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // intilaize both the array X and Y coords double y[] = { -2.8, 7.3 }; float x[] = { 5.0, -0.8, 3.2, 5, 1 }; // intilaize both the valarray X and Y coords valarray< double > ycoords(y, 2); valarray< float > xcoords(x, 4); // store results in valarray res valarray< double > res = atan2 (ycoords, xcoords); // print results of atan2() function cout << "results:" ; for ( size_t i = 0; i < res.size(); ++i) cout << ' ' << res[i]; cout << '
' ; return 0; } |
Выход:
prog.cpp: В функции int main (): prog.cpp: 14: 48: ошибка: нет соответствующей функции для вызова 'atan2 (std :: valarray &, std :: valarray &)' valarray res = atan2 (ycoords, xcoords); ^