版本比较
C++ sin/cos详细介绍
正文
增加 46 行删除 46 行
25 行未改动262728293030313233348 行未改动434445464747484950519 行未改动616263646565666768697071717273747576777778798081828384858687878889909191929394956 行未改动1021031041051061061071081091101111111121131141158 行未改动1241251261271281281291301311321331341351361371381381391401411421431441441451461471487 行未改动1561571581591601601611621631641651651661671681691701711721731741741751761771781791791801811821831841851861871881891891901911921931931941951961971981992002012022022032042052064 行未改动2112122132142152152162172172182192202212212222232242259 行未改动2352362372382392392402412422432 行未改动2462472482492502502512522532543 行未改动25825926026126226326226326426526626726826826927027127227327327427527627627727827928012 行未改动29329429529629729729829930030130230230330430530630730830930931031131231331431431531631731831932032132132232332432532632632732832933033133233333333433533633711 行未改动3493503513523533533543553563574 行未改动3623633643653663663673683693693703713723737 行未改动3813823833843853853863873883893903913923933943943953963973983994004014024034034044054064077 行未改动41541641741841941942042142242342442442542642742742842943043143243343343443543643711 行未改动25 行未改动double cos(double x); // 计算弧度x的余弦值float sinf(float x); // 单精度版本long double sinl(long double x); // 长双精度版本
float cosf(float x); // 单精度版本 float cosf(float x); // 单精度版本long double cosl(long double x); // 长双精度版本```
### 参数说明8 行未改动double degreesToRadians(double degrees) { return degrees * PI / 180.0;}
// 弧度转角度 // 弧度转角度double radiansToDegrees(double radians) { return radians * 180.0 / PI;}```9 行未改动using namespace std;
int main() { const double PI = 3.14159265358979323846;
// 基本计算 cout << "sin(0) = " << sin(0) << endl; cout << "cos(0) = " << cos(0) << endl; cout << "sin(PI/2) = " << sin(PI/2) << endl; cout << "cos(PI/2) = " << cos(PI/2) << endl;
// 角度计算 double angle_deg = 45.0; double angle_rad = angle_deg * PI / 180.0; cout << "sin(45°) = " << sin(angle_rad) << endl; cout << "cos(45°) = " << cos(angle_rad) << endl;
return 0;}```
### 三角函数关系验证```cppvoid verifyTrigonometricRelations() { const double PI = 3.14159265358979323846; double x = PI / 6; // 30度
// 平方和关系: sin²x + cos²x = 1 double sum_of_squares = pow(sin(x), 2) + pow(cos(x), 2); cout << "sin²(π/6) + cos²(π/6) = " << sum_of_squares << endl;
// 互补角关系: sin(x) = cos(π/2 - x) double complementary_sin = sin(x); double complementary_cos = cos(PI/2 - x); cout << "sin(π/6) = " << complementary_sin << endl;6 行未改动```cppvoid generateSineWave() { const double PI = 3.14159265358979323846; const int samples = 20;
cout << "正弦波形:" << endl; for (int i = 0; i < samples; ++i) { double angle = 2 * PI * i / samples; double value = sin(angle);
// 可视化波形 int bar_length = static_cast<int>((value + 1) * 10); cout << setw(3) << i << ": " << string(bar_length, '*') << endl; }8 行未改动private: static const int TABLE_SIZE = 360; double sin_table[TABLE_SIZE]; double cos_table[TABLE_SIZE];
public: TrigTable() { const double PI = 3.14159265358979323846; for (int i = 0; i < TABLE_SIZE; ++i) { double radians = i * PI / 180.0; sin_table[i] = sin(radians); cos_table[i] = cos(radians); } }
double fast_sin(int degrees) { degrees = degrees % 360; if (degrees < 0) degrees += 360; return sin_table[degrees]; }
double fast_cos(int degrees) { degrees = degrees % 360; if (degrees < 0) degrees += 360; return cos_table[degrees];7 行未改动double taylor_sin(double x, int terms = 10) { double result = 0.0; double term = x; double x_squared = x * x;
for (int n = 1; n <= terms; ++n) { result += term; term *= -x_squared / ((2*n) * (2*n+1)); }
return result;}
// 使用泰勒级数计算cos(x)double taylor_cos(double x, int terms = 10) { double result = 1.0; double term = 1.0; double x_squared = x * x;
for (int n = 1; n <= terms; ++n) { term *= -x_squared / ((2*n-1) * (2*n)); result += term; }
return result;}```
### 3. 范围缩减优化```cppdouble optimized_sin(double x) { const double PI = 3.14159265358979323846; const double TWO_PI = 2.0 * PI;
// 将角度缩减到 [0, 2π] 范围内 x = fmod(x, TWO_PI); if (x < 0) x += TWO_PI;
// 利用对称性缩减到 [0, π/2] if (x > PI) { x = TWO_PI - x; return -sin(x); } if (x > PI/2) { x = PI - x; }
return sin(x);}```
4 行未改动#include <complex>
void complexTrigonometry() { using namespace std::complex_literals;
std::complex<double> z = 1.0 + 1.0i;
// 欧拉公式: e^(ix) = cos(x) + i*sin(x) std::complex<double> euler = std::exp(1.0i * M_PI); cout << "e^(iπ) = " << euler << endl;
// 复数三角函数 std::complex<double> csin = std::sin(z); std::complex<double> ccos = std::cos(z); cout << "sin(1+i) = " << csin << endl;9 行未改动
Point2D rotatePoint(const Point2D& point, double angle_rad) { double cos_a = cos(angle_rad); double sin_a = sin(angle_rad);
return { point.x * cos_a - point.y * sin_a, point.x * sin_a + point.y * cos_a };2 行未改动void rotationDemo() { Point2D p = {1.0, 0.0}; double angle_deg = 90.0; double angle_rad = angle_deg * M_PI / 180.0;
Point2D rotated = rotatePoint(p, angle_rad); cout << "旋转前: (" << p.x << ", " << p.y << ")" << endl; cout << "旋转后: (" << rotated.x << ", " << rotated.y << ")" << endl;}3 行未改动```cppclass SignalGenerator {public: // 生成正弦信号 static vector<double> generateSineWave(double amplitude, double frequency, double phase, double sample_rate, static vector<double> generateSineWave(double amplitude, double frequency, double phase, double sample_rate, double duration) { int samples = static_cast<int>(sample_rate * duration); vector<double> signal(samples); double angular_freq = 2 * M_PI * frequency;
for (int i = 0; i < samples; ++i) { double t = i / sample_rate; signal[i] = amplitude * sin(angular_freq * t + phase); }
return signal; }
// 计算信号的RMS值 static double calculateRMS(const vector<double>& signal) { double sum_squares = 0.0; for (double value : signal) {12 行未改动
class Matrix3x3 {public: double data[3][3];
// 绕X轴旋转 static Matrix3x3 rotationX(double angle) { double c = cos(angle); double s = sin(angle);
return {{ {1, 0, 0}, {0, c, -s}, {0, s, c} }}; }
// 绕Y轴旋转 static Matrix3x3 rotationY(double angle) { double c = cos(angle); double s = sin(angle);
return {{ {c, 0, s}, {0, 1, 0}, {-s, 0, c} }}; }
// 绕Z轴旋转 static Matrix3x3 rotationZ(double angle) { double c = cos(angle); double s = sin(angle);
return {{ {c, -s, 0}, {s, c, 0}, {0, 0, 1} }}; }
Vector3 operator*(const Vector3& vec) const { return { data[0][0]*vec.x + data[0][1]*vec.y + data[0][2]*vec.z, data[1][0]*vec.x + data[1][1]*vec.y + data[1][2]*vec.z,11 行未改动 // 大角度的精度损失 double large_angle = 1e10; cout << "sin(1e10) = " << sin(large_angle) << endl; cout << "精度受影响,建议使用范围缩减" << endl;
// 解决方案:定期范围缩减 double optimized = optimized_sin(large_angle); cout << "优化后: " << optimized << endl;}4 行未改动void specialValues() { // 处理无穷大和NaN double inf = numeric_limits<double>::infinity(); double nan = numeric_limits<double>::quiet_NaN();
cout << "sin(INF) = " << sin(inf) << " (返回NaN)" << endl; cout << "sin(NaN) = " << sin(nan) << " (返回NaN)" << endl;
// 检查数学错误 errno = 0; double result = sin(inf); if (errno != 0) {7 行未改动#include <chrono>
void performanceComparison() { const int iterations = 1000000;
// 标准sin函数 auto start = chrono::high_resolution_clock::now(); double sum1 = 0.0; for (int i = 0; i < iterations; ++i) { sum1 += sin(i * 0.001); } auto end = chrono::high_resolution_clock::now(); auto duration1 = chrono::duration_cast<chrono::microseconds>(end - start);
// 泰勒级数近似 start = chrono::high_resolution_clock::now(); double sum2 = 0.0; for (int i = 0; i < iterations; ++i) { sum2 += taylor_sin(i * 0.001, 5); } end = chrono::high_resolution_clock::now(); auto duration2 = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "标准sin: " << duration1.count() << " μs" << endl; cout << "泰勒近似: " << duration2.count() << " μs" << endl; cout << "精度差异: " << abs(sum1 - sum2) << endl;}7 行未改动 if (!isfinite(x)) { cerr << "错误: 输入值不是有限数" << endl; return nullopt; }
// 处理极大值可能导致的精度问题 if (abs(x) > 1e8) { cerr << "警告: 大角度可能影响精度" << endl; }
return sin(x); }
static optional<double> safe_cos(double x) { if (!isfinite(x)) { cerr << "错误: 输入值不是有限数" << endl; return nullopt; }
return cos(x); }};```11 行未改动