我是否错误地使用了Xcode或编写了错误的程序? 所以我正在做一个关于正弦的问题C++ < /P>

我是否错误地使用了Xcode或编写了错误的程序? 所以我正在做一个关于正弦的问题C++ < /P>,c++,C++,它说sinx可以通过多项式x-(x^3/6)+(x^5/120)-(x^7/5040)来近似,它告诉我输出近似的sin值和通过cmath计算的sin值 输入以度为单位,我们必须首先将其转换为弧度,然后找出sin 示例运行(只有45是输入,其他是输出): 角度:45 approxSin=0.70710647 cmath sin=0.70710678 我试图为此编写一个代码。当我按下command+R时,尽管程序说“build successful”,但什么也没有发生。我是新来的Xcode,所以我不

它说sinx可以通过多项式x-(x^3/6)+(x^5/120)-(x^7/5040)来近似,它告诉我输出近似的sin值和通过cmath计算的sin值

输入以度为单位,我们必须首先将其转换为弧度,然后找出sin

示例运行(只有45是输入,其他是输出):

角度:45 approxSin=0.70710647 cmath sin=0.70710678

我试图为此编写一个代码。当我按下command+R时,尽管程序说“build successful”,但什么也没有发生。我是新来的Xcode,所以我不确定我是否用错了Xcode或者我写错了程序。有人能帮忙吗

#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double approxSin(double angleDeg) {
    if (-180<angleDeg<180) return approxSin(angleDeg-(angleDeg*angleDeg*angleDeg)/6+(angleDeg*angleDeg*angleDeg*angleDeg*angleDeg)/120-(angleDeg*angleDeg*angleDeg*angleDeg*angleDeg*angleDeg*angleDeg)/5040);
}
int main(){
    float angleDeg;
    cin >> angleDeg;
    if (angleDeg>180) {
        while (angleDeg>180) {
            angleDeg = angleDeg-360;
        }
    } else if (angleDeg<-180) {
        while (angleDeg<-180) {
            angleDeg = angleDeg+360;
        }
    }
    cout << "approxSin = " << &approxSin << endl;
    cout << "cmath sin = " << setprecision(8) << sin(angleDeg);
    return 0;
}
定义使用数学定义
#包括
#包括
#包括
使用名称空间std;
双进近(双角度){
如果(-180

我的猜测你的问题:你运行程序,它耐心地等待你的输入

您的程序在等待您在IDE控制台窗口中提供一些输入时似乎停止了。由于您没有编写任何提示,因此没有输出来告诉您它正在等待输入

我建议您首先添加一些输出以请求输入:

cout << "Please enter angle in degrees: ";
cin >> angleDeg;
cout>angleDeg;
当我按下command+R时,尽管程序说“build successful”,但什么也没有发生

我猜想,BY应该解决这个问题,但是,正如注释中所提到的,在发布的代码中有更严重的问题,这可能是由于对函数如何在C++中声明和调用的误解。 考虑这一点:

double approxSin(double angleDeg) {
    if (-180<angleDeg<180) return approxSin(/* Some unreadable expression */);
}
它最终打印出
1
,原因可在本问答中找到:

最后,我要注意的是,虽然分配特别要求将输入的角度从度转换为弧度(正如
std::sin
的参数所示),但发布的代码只检查以度为单位的范围,没有任何转换

下面的实现比较了计算sin()函数的不同方法

#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>

namespace my {

// M_PI while widespread, isn't part of the ISO standard    
#ifndef M_PI
constexpr double pi = 3.141592653589793115997963468544185161590576171875;
#else
constexpr double pi = M_PI;
#endif    

constexpr double radians_from_degrees(double degrees)
{
    return degrees * pi / 180.0;
}

constexpr double convert_angle_to_plus_minus_pi(double angle)
{
    while ( angle < -pi )
        angle += 2.0 * pi;
    while ( angle > pi ) {
        angle -= 2.0 * pi;
    }
    return angle;
}

// Approximates sin(angle), with angle between [-pi, pi], using a polynomial
// Evaluate the polynomial using Horner's method
constexpr double sin_a(double angle)
{
    // A radian is passed, but the approximation is good only in [-pi, pi]
    angle = convert_angle_to_plus_minus_pi(angle);

    // Evaluates p(a) = a - a^3 / 6 + a^5 / 120 - a^7 / 5040
    double sq_angle = angle * angle;
    return angle * ( 1.0 + sq_angle * (-1.0/6.0 + sq_angle * ( 1.0/120.0 - sq_angle / 5040.0)));
}

double sin_b(double angle) {
    angle = convert_angle_to_plus_minus_pi(angle);
    return angle - pow(angle, 3) / 6.0 + pow(angle, 5) / 120.0 - pow(angle, 7) / 5040.0;
}

} // End of namespace 'my'

int main()
{
    std::cout << " angle    std::sin      my::sin_a     my::sin_b\n"
              << "-----------------------------------------------\n"
              << std::setprecision(8) << std::fixed;

    for (int i = -90; i < 475; i += 15)
    {
        double angle = my::radians_from_degrees(i);

        std::cout << std::setw(5) << i
                  << std::setw(14) << std::sin(angle)
                  << std::setw(14) << my::sin_a(angle)
                  << std::setw(14) << my::sin_b(angle) << '\n';
    }
    return 0;
}
定义使用数学定义
#包括
#包括
#包括
名称空间我的{
//M_PI虽然广泛存在,但不是ISO标准的一部分
#伊夫德夫·穆皮
constexpr double pi=3.141592653589793115997963468544185161590576171875;
#否则
constexpr-double-pi=M_-pi;
#恩迪夫
constexpr双弧度(双度)
{
返回度*pi/180.0;
}
constexpr双角度转换为双角度
{
同时(角度<-pi)
角度+=2.0*pi;
同时(角度>圆周率){
角度-=2.0*pi;
}
返回角;
}
//使用多项式近似sin(角度),角度介于[-pi,pi]之间
//用霍纳方法求多项式
constexpr双正弦(双角度)
{
//弧度通过,但近似值仅在[-pi,pi]中有效
角度=将角度转换为角度加上减去pi(角度);
//计算p(a)=a-a^3/6+a^5/120-a^7/5040
双平方角=角*角;
返回角*(1.0+sq_角*(-1.0/6.0+sq_角*(1.0/120.0-sq_角/5040.0));
}
双正弦b(双角度){
角度=将角度转换为角度加上减去pi(角度);
返回角-功率(角度,3)/6.0+功率(角度,5)/120.0-功率(角度,7)/5040.0;
}
}//命名空间“my”的结尾
int main()
{

std::我可以建议你退一步,回到你的书、教程或课堂笔记吗?因为你不调用你的函数
approxSin
。但是你确实正确地调用了标准
sin
函数,所以你清楚地知道如何调用函数。你怎么会认为你定义的函数之间有区别好的,还有一个标准函数?如果你没有书的话。
可以
(-180
-180条件
-180我这样做了,但我得到的信息是,我的代码没有返回任何内容。我做错了哪一部分?@AMU是的:。根据你的需要调整它应该不难。如果有什么不清楚的地方,请告诉我。
prog.cc:7:22: warning: result of comparison of constant 180 with expression of type 'bool'
                       is always true [-Wtautological-constant-out-of-range-compare]
    if (-180<angleDeg<180) return approxSin(angleDeg-(...));
        ~~~~~~~~~~~~~^~~~
prog.cc:6:35: warning: all paths through this function will call itself [-Winfinite-recursion]
double approxSin(double angleDeg) {
                                  ^
cout << "approxSin = " << &approxSin << endl;
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>

namespace my {

// M_PI while widespread, isn't part of the ISO standard    
#ifndef M_PI
constexpr double pi = 3.141592653589793115997963468544185161590576171875;
#else
constexpr double pi = M_PI;
#endif    

constexpr double radians_from_degrees(double degrees)
{
    return degrees * pi / 180.0;
}

constexpr double convert_angle_to_plus_minus_pi(double angle)
{
    while ( angle < -pi )
        angle += 2.0 * pi;
    while ( angle > pi ) {
        angle -= 2.0 * pi;
    }
    return angle;
}

// Approximates sin(angle), with angle between [-pi, pi], using a polynomial
// Evaluate the polynomial using Horner's method
constexpr double sin_a(double angle)
{
    // A radian is passed, but the approximation is good only in [-pi, pi]
    angle = convert_angle_to_plus_minus_pi(angle);

    // Evaluates p(a) = a - a^3 / 6 + a^5 / 120 - a^7 / 5040
    double sq_angle = angle * angle;
    return angle * ( 1.0 + sq_angle * (-1.0/6.0 + sq_angle * ( 1.0/120.0 - sq_angle / 5040.0)));
}

double sin_b(double angle) {
    angle = convert_angle_to_plus_minus_pi(angle);
    return angle - pow(angle, 3) / 6.0 + pow(angle, 5) / 120.0 - pow(angle, 7) / 5040.0;
}

} // End of namespace 'my'

int main()
{
    std::cout << " angle    std::sin      my::sin_a     my::sin_b\n"
              << "-----------------------------------------------\n"
              << std::setprecision(8) << std::fixed;

    for (int i = -90; i < 475; i += 15)
    {
        double angle = my::radians_from_degrees(i);

        std::cout << std::setw(5) << i
                  << std::setw(14) << std::sin(angle)
                  << std::setw(14) << my::sin_a(angle)
                  << std::setw(14) << my::sin_b(angle) << '\n';
    }
    return 0;
}