Visual C++에서 삼각 STL 함수 사용
아래 샘플 코드는 Visual C++에서 STLacos
, , asin
, atan
atan2
, cos
, cosh
, sin
, , sinh
, 함수 tanh
tan
를 사용하는 방법을 보여 줍니다. 이 문서의 정보는 관리되지 않는 Visual C++ 코드에만 적용됩니다.
원래 제품 버전: Visual C++
원본 KB 번호: 157950
필수 헤더
<valarray>
<cmath>
프로토 타입
// acos
template <class T>
inline valarray<T> acos(const valarray<T> &x);
// asin
template <class T>
inline valarray<T> asin(const valarray<T> &x);
// atan
template <class T>
inline valarray<T> atan(const valarray<T> &x);
// atan2
template <class T>
inline valarray<T> atan2(const valarray<T> &x, const valarray<T> &y);
template <class T>
inline valarray<T> atan2(const valarray<T> x, const T &y);
template <class T>
inline valarray<T> atan2(const T &x, const valarray<T> &y);
// cos
template <class T>
inline valarray<T> cos(const valarray<T> &x);
// cosh
template <class T>
inline valarray<T> cosh(const valarray<T> &x);
// sin
template <class T>
inline valarray<T> sin(const valarray<T> &x);
// sinh
template <class T>
inline valarray<T> sinh(const valarray<T> &x);
// tan
template <class T>
inline valarray<T> tan(const valarray<T> &x);
// tanh
template <class T>
inline valarray<T> tanh(const valarray<T> &x);
참고
프로토타입의 클래스 또는 매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.
샘플 코드
이 문서에서는 다음 샘플 코드를 통해 STL 삼각 함수를 사용하는 것을 보여 줍니다.
//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// main.cpp : Illustrates the use of STL trigonometry functions.
// Functions:
// acos, asin, atan, atan2, cos, cosh, sin, sinh, tan, tanh
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
#include <iostream> // for i/o functions
#include <valarray> // for valarray
#include <cmath> // for trigonometry functions
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#define ARRAY_SIZE 3 // array size
void main()
{
// Initialize val_array to values -1, 0 and 1.
valarray<double> val_array(ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
val_array[i] = i - 1;
// Display the size of val_array.
cout << "Size of val_array = " << val_array.size() << endl;
// Display the values of val_array before calling any trigonometry
// functions.
cout << "The values in val_array:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << val_array[i] << " ";
cout << endl << endl;
// Initialize rev_valarray that is the reverse of val_array.
valarray<double> rev_valarray(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
rev_valarray[i] = val_array[ARRAY_SIZE - i - 1];
// Display the size of rev_valarray.
cout << "Size of rev_valarray = " << rev_valarray.size() << endl;
// Display the values of rev_valarray.
cout << "The values in rev_valarray:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rev_valarray[i] << " ";
cout << endl << endl;
// rvalue_array to hold the return value from calling the trigonometry
// functions.
valarray<double> rvalue_array;
// acos() - display the result of rvalue_array
rvalue_array = acos(val_array);
cout << "The result after calling acos():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// asin() - display the result of rvalue_array
rvalue_array = asin(val_array);
cout << "The result after calling asin():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// atan() - display the result of rvalue_array
rvalue_array = atan(val_array);
cout << "The result after calling atan():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// atan2() - display the result of rvalue_array
// This template function returns an object of class valarray<T>,
// each of whose elements at I is the arctangent of x[I] / y[I].
rvalue_array = atan2(val_array, rev_valarray);
cout << "The result after calling atan2(val_array, rev_valarray):"
<< endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// This template function stores in element I the arctangent of
// x[I] / y.
rvalue_array = atan2(val_array, 3.1416);
cout << "The result after calling atan2(val_array, 3.1416):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// This template function stores in element I the arctangent of
// x / y[I].
rvalue_array = atan2(3.1416, val_array);
cout << "The result after calling atan2(3.1416, val_array):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// cos() - display the result of rvalue_array
rvalue_array = cos(val_array);
cout << "The result after calling cos():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// cosh() - display the result of rvalue_array
rvalue_array = cosh(val_array);
cout << "The result after calling cosh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// sin() - display the result of val_array
rvalue_array = sin(val_array);
cout << "The result after calling sin():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// sinh() - display the result of val_array
rvalue_array = sinh(val_array);
cout << "The result after calling sinh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// tan() - display the result of val_array
rvalue_array = tan(val_array);
cout << "The result after calling tan():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
// tanh() - display the result of val_array
rvalue_array = tanh(val_array);
cout << "The result after calling tanh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
}
프로그램 출력
Size of val_array = 3
The values in val_array:
-1 0 1
Size of rev_valarray = 3
The values in rev_valarray:
1 0 -1
The result after calling acos():
3.14159 1.5708 0
The result after calling asin():
-1.5708 0 1.5708
The result after calling atan():
-0.785398 0 0.785398
The result after calling atan2(val_array, rev_valarray):
-0.785398 0 2.35619
The result after calling atan2(val_array, 3.1416):
-0.308168 0 0.308168
The result after calling atan2(3.1416, val_array):
1.87896 1.5708 1.26263
The result after calling cos():
0.540302 1 0.540302
The result after calling cosh():
1.54308 1 1.54308
The result after calling sin():
-0.841471 0 0.841471
The result after calling sinh():
-1.1752 0 1.1752
The result after calling tan():
-1.55741 0 1.55741
The result after calling tanh():
-0.761594 0 0.761594
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기