- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Code::Blocks 10.05 编译这个程序,但是通常我会在它开始在每个输出中生成 Nan 之前完成大约 10 次迭代。我想知道这是否是使用 cos 和 sin 函数引起的问题,是否有适当的解决方法来避免这种情况?
我必须进行大量迭代,因为我正在为大学做一个项目,所以它也必须准确。我查阅了一些关于如何避免使用 sin 和 cos 的文章,尽管我需要严格遵循一些公式,否则我产生的结果可能不准确,所以我不确定是否妥协。
struct Particle // Need to define what qualities our particle has
{
double dPosition;
double dAngle;
};
Particle Subject;
void M1(double &x, double &y) //Defines movement if particle doesn't touch inner boundary
{
x = x + 2*y;
}
double d = 0.25; //This can and will be changed when I need to find a distance between
// the two cricles at a later stage
void M2(double &x,double &y, double d) //Defines movement of a particle if it impacts the inner boundary
{
double z = asin(-(sin(y)+d*cos(x + y))/0.35);
double y1 = y;
y = asin(-0.35*sin(z) + d*cos(x + y + 2*z));
x = y + y1 + x + 2*z;
}
int main()
{
cout << "Please tell me where you want this particle to start positions-wise? (Between 0 and 2PI" << endl;
cin >> Subject.dPosition;
cout << "Please tell me the angle that you would like it to make with the normal? (Between 0 and PI/2)" << endl;
cin >> Subject.dAngle;
cout << "How far would you like the distances of the two middle circles to be?" << endl;
double d;
cin >> d;
// These two functions are to understand where the experiment begins from.
// I may add a function to change where the circle starts however I will use radius = 0.35 throughout
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
int n=0;
while (n <= 100) //This is used to iterate the process and create an array of Particle data points
{ // in order to use this data to build up Poincare diagrams.
{
while (Subject.dPosition > 2*M_PI)
Subject.dPosition = Subject.dPosition - 2*M_PI;
}
{
if (0.35 >= abs(0.35*cos(Subject.dPosition + Subject.dAngle)+sin(Subject.dAngle))) //This is the condition of hitting the inner boundary
M2(Subject.dPosition, Subject.dAngle, d); //Inner boundary collision
else
M1(Subject.dPosition, Subject.dAngle); // Outer boundary collision
};
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
n++;
}
return 0;
}
最佳答案
Nan
在 c++ 中表示无限、零差和不可表示数的一些其他变体。
编辑:
正如 Matteo Itallia 所指出的,inf
用于无限/零除法。我发现了这些方法:
template<typename T>
inline bool isnan(T value) {
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value) {
return std::numeric_limits<T>::has_infinity &&
value == std::numeric_limits<T>::infinity();
}
引用:http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan
关于c++ - 使用 sin 和 cos 函数迭代时的 Nan 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14007514/
我刚开始研究 C#,冗长的数学函数让我很烦。是否有标准的方法来编写 Cos(或任何其他 Math 函数)而不是 Math.Cos 而无需定义您自己的 Cos 函数? 最佳答案 切换到新的 Roslyn
我通过 R 运行 WolframAlpha Wolfram Alpha API from R 我的问题是,我需要将 wolfram 输出转换为 R 表达式。我在需要的地方添加了“*”,还有另一个问题
我遇到了一些应该很容易回答的问题,但我不能 Handlebars 指放在上面。自从我做了一些三角函数以来已经有一段时间了。 double cosValue = -2.7105054312E-20; /
如何重现此行为? https://isocpp.org/wiki/faq/newbie#floating-point-arith2 准确的说,在下面的代码中,参数x和y是相等的;它们可以等于 1.0
JavaScript 中的所有数学函数都使用弧度代替度数。然而,它们要么不相等,要么我离题太远。 从度数到弧度的转换是: var rad = angle * Math.PI / 180 90 度 An
这个问题已经有答案了: How can I get sin, cos, and tan to use degrees instead of radians? (6 个回答) 已关闭 9 年前。 cos
这是怎么回事?? 测试 sin 和 cos 函数以找出为什么在将我的坐标输出到 SVG 文件时我在错误的位置得到如此漂亮的定位。所以我做了这个测试代码,我可以预测答案是什么来找出原因。奇怪的是,没有任
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: C Build error when getting the value of sin() 我试图在这段时间内
在 Javascript 中使用 a.b(c) 或 [a.b][0](c) 是不同的,原因是 this 的绑定(bind)> 在 a.b 的代码执行期间是否发送给对象 a。 使用相同的推理 var z
这是一个很简单的问题,让我很困惑。 我收到一个源文件的以下错误,但另一个没有: 4 src/Source2.cpp:1466: error: no matching function for cal
如何只使用 sin 或 cos 而不是 Math.sin 或 Math.cos?我尝试导入 Math.* 但我想我可能需要对命名空间做一些事情? 最佳答案 import static java.lan
测试代码: #include #include const int N = 4096; const float PI = 3.1415926535897932384626; float cosin
代码片段1: from sympy import symbols, integrate, cos, pi from numpy import sqrt n = symbols('n', integer
我正在使用 Taylors series 来计算一个数字的 cos,对于小数字,该函数返回准确的结果,例如 cos(5) 给出 0.28366218546322663 。但是对于较大的数字,它会返回不
我是一个完全的编程初学者,我被分配了以下任务: 编写一个 C++ 程序,使用一系列内接和外接正多边形计算一对 π 的估计值。在不超过 30 步后停止,或者当外接多边形和内切多边形的周长之差小于 ε=1
我正在尝试使用余弦和正弦,但它们没有返回我期望的值。 #include #include #include int main() { float magnitudeForce;
我有一个作业要编写一个程序来通过 Maclaurin approximation 计算 cos(x) .但是,我必须为 cos(x) 使用一个函数,并使用另一个函数来计算 cos(x) 函数内的分母上
我正在尝试使用 Python2.7 对方程式进行数值求解。这是整个代码: from sympy import * from sympy import Symbol from sympy.solvers
因此,我正在尝试创建一个使用泰勒近似计算 cos(x) 的程序。 程序非常简单:用户输入一个参数 x(x 是以弧度为单位的角度)和一个 float ε,它是 cos(x) 值的精度。 基本上,程序唯一
我正在尝试将内部 cos() 函数调用插入 LLVM 传递。我在 FunctionPass 中的代码: std::vector arg_type; arg_type.push_back(Type::g
我是一名优秀的程序员,十分优秀!