Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c++ - 创建双曲时的怪异行为(Bizarre behavior when creating a double)

I wrote a program that simulates perfectly elastic collisions in 1D using an event-driven algorithm.

(我编写了一个程序,该程序使用事件驱动算法模拟一维完美的弹性碰撞。)

The problem is something akin to Newton's cradle.

(问题出在牛顿的摇篮上。)

I was trying to fix something I perceive to be an issue, which is that when I give two spheres an equal initial velocity, the position and velocity arrays are updated twice due to simultaneous collisions (and thus I get a double output for the same instant).

(我试图解决一个我认为是问题的问题,那就是当我赋予两个球体相等的初始速度时,由于同时发生碰撞,位置和速度阵列会更新两次(因此,我在同一瞬间获得了双重输出) )。)

To this effect, I created a variable that would check whether the next collision would happen within 0 seconds (ie: "now").

(为此,我创建了一个变量,该变量将检查下一次碰撞是否会在0秒内发生(即:“现在”)。)

However, when I do that, the time until the collision changes completely for those simultaneous collisions.

(但是,当我这样做时,对于那些同时发生的碰撞,直到碰撞完全改变的时间。)

There are 5 particles in total, and each one has a radius of 1, with a separation of 0.5 between them.

(总共有5个粒子,每个粒子的半径为1,它们之间的间隔为0.5。)

Collisions are perfectly elastic, gravity is disregarded, and the algorithm stops when the final particle hits a wall placed arbitrarily in front of it.

(碰撞具有完全的弹性,可以忽略重力,并且当最终粒子撞击任意放置在其前面的墙时,算法将停止。)

Initial velocity for the first two particles is 1, so the first collision should occur after 0.5, and the second and third collisions should occur simultaneously after another 0.5.

(前两个粒子的初始速度为1,因此第一次碰撞应在0.5之后发生,第二次和第三次碰撞应在另外0.5次之后同时发生。)

Before adding the variable to check whether or not the time until collision is 0, the time until collision was outputted as 6.94906e-310 (this was verified by outputting the return value of the function that calculates the time until collision).

(在添加变量以检查直到碰撞的时间是否为0之前,将直到碰撞的时间输出为6.94906e-310(通过输出计算到碰撞的时间的函数的返回值进行验证)。)

With the new variable that was going to be used to check if the previous value was zero, the time until collision during a simultaneous collision is now outputted as -1 in both the new variable and the return value of the aforementioned function.

(使用将用于检查先前值是否为零的新变量,现在在新变量和上述函数的返回值中,在同时碰撞期间发生碰撞之前的时间现在都作为-1输出。)

I'm guessing this has something to do with the fact that it's an extremely small double value, but I don't quite understand the problem.

(我猜这与一个很小的double值有关,但是我不太了解这个问题。)

How could creating one variable affect the value of another (albeit somewhat related) variable to this extent?

(创建一个变量如何在此程度上影响另一个(尽管有些相关)变量的值?)

The code below is just to help visualize the problem.

(下面的代码仅用于帮助可视化问题。)

It is not a MWE .

(它不是MWE 。)

I'd have to post almost my entire code here to produce a MWE.

(我必须在这里发布几乎所有的代码才能生成MWE。)

Because this is for a class, I could get plagiarized or be accused of plagiarism myself, so I don't feel comfortable posting it.

(因为这是一堂课,所以我自己可能会被窃或被指控为窃,因此发布此书令我感到不自在。)

Again, not a MWE, just to better explain what I'm doing.

(同样,不是MWE,而是为了更好地解释我在做什么。)

//x and v are arrays with the positions and
//velocities, respectively, of each particle
//hasReachedWall(x) checks if the last particle has hit the wall
while (!hasReachedWall(x)) {
    //The return value of updatePos(x, v) is a double with the time until the next collision
    //aux, is the cause of the problem. Its existence somehow changes
    //the return value into -1 when it should be near-zero. Without this variable,
    //the return value is as expected (near-zero)
    double aux = updatePos(x, v);
    cout << aux << endl;
    //t is also a double
    t += aux;
}

EDIT: I am aware of how doubles are stored internally and that operations performed on them have errors.

(编辑:我知道如何在内部存储双精度,并且对其执行的操作有错误。)

My problem is that the mere creation of an intermediary variable completely changes the result of an operation.

(我的问题是仅创建一个中间变量会完全改变操作的结果。)

I'm creating a double to store the return value of a function (another double - not long double, just double), and the return value of the function changes radically.

(我正在创建一个double来存储函数的返回值(另一个double- 不是 long double,只是double),并且该函数的返回值发生了根本变化。)

I don't understand why.

(我不明白为什么。)

  ask by Orion translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...