关于浮点数相等判断
-
我想编写一段代码,实现指点时间,指定地点发生某事.具体方案是随机生成时间,地点的坐标,存在某个向量里.
然后随着求解器时间推进,每次遍历上述向量,看向量里的代表时间的值是否和runTime.value()相等,决定某事是否发生.
但是在实际编程时,始终无法得到预期的结果,请各位帮忙看看
//将时间和坐标进行关联 struct coordinateTime { scalar breakTime; scalar xCoordinate; scalar yCoordinate; }; // 获取top这个边界面的面积 std::vector<coordinateTime> timeCoordinateVector {}; label patchID = mesh.boundaryMesh().findPatchID("top"); const polyPatch& cPatch = mesh.boundaryMesh()[patchID]; const surfaceScalarField& magSf = mesh.magSf(); scalar patchArea = 0.0; forAll(cPatch,faceI) { patchArea += magSf.boundaryField()[patchID][faceI]; } Info << "Patch Area " << patchArea << endl; //获取模拟总时间 scalar totalTime = runTime.controlDict().lookupOrDefault<scalar>("endTime", 50.); Info << "total Time = " << nl << totalTime << endl; //获取模拟的时间间隔 scalar deltatime = runTime.controlDict().lookupOrDefault<scalar>("deltaT", 0.1); //计算事件发生的总次数,ceil用于取整 label totalBreaker = ceil(0.00001*totalTime*patchArea); Info << "total Number of breaker = " << nl << totalBreaker << endl; //生成服从均匀分布的破碎初始坐标和发生时间 Random ranGen(clock::getTime()+pid()); scalar xIntial,yIntial; //坐标范围 //x范围 scalar a=0., b=14.08; //y范围 scalar g = 0.,h = 7.04; //时间范围 scalar e = 0.,f = totalTime; //时间精度 scalar scale = 0.1; struct coordinateTime coordinateTime1; for(label i = 0;i<totalBreaker;i++) { coordinateTime1.xCoordinate = a+b*(random()/(RAND_MAX+1.)); coordinateTime1.yCoordinate = g+h*(random()/(RAND_MAX+1.)); coordinateTime1.breakTime = f*random()/(RAND_MAX+1.); coordinateTime1.breakTime = floor(coordinateTime1.breakTime/scale+0.5)*scale; timeCoordinateVector.push_back(coordinateTime1); } Info<<"the size of timeCoordinateVector vector :"<<timeCoordinateVector.size()<<endl; for(auto x: timeCoordinateVector) { Info<<x.breakTime<<" "<<x.xCoordinate<<" "<<x.yCoordinate<<endl; } while (runTime.run()) { Info << "Running... " << "Time = " << runTime.value() << endl; for(auto x:timeCoordinateVector) { if(abs(runTime.value()-x.breakTime)<1e-6) { Info<<"break happen"<<endl; } } runTime++; }
某次测试的结果.比如说在时间等于0的时刻,不应该发生该事件,但却发生了.
我的代码编写什么地方有问题?
某次测试结果
the size of timeCoordinateVector vector :18 15.7 11.8298 2.77646 4 11.242 6.418 5.6 4.71994 5.40834 12.6 7.7999 3.36088 19 5.13617 3.61434 14.3 12.9 4.47541 0.3 1.99376 4.27306 16.1 3.41985 0.96611 2.6 2.20604 2.82265 4.4 1.53203 7.03243 12.3 7.22209 5.90735 10.5 4.16813 4.48837 5.9 6.94965 6.84834 15.4 10.8607 3.70828 5.7 5.63522 6.27637 18.4 4.96261 5.68638 10.5 0.982154 6.68326 13.3 1.21167 1.35319 Running... Time = 0 break happen Running... Time = 0.1 break happen Running... Time = 0.2 break happen Running... Time = 0.3 break happen Running... Time = 0.4 break happen Running... Time = 0.5 break happen Running... Time = 0.6 break happen Running... Time = 0.7 break happen Running... Time = 0.8 break happen Running... Time = 0.9 break happen Running... Time = 1 break happen Running... Time = 1.1 break happen Running... Time = 1.2 break happen Running... Time = 1.3 Running... Time = 1.4 Running... Time = 1.5 Running... Time = 1.6 break happen Running... Time = 1.7 break happen Running... Time = 1.8 break happen Running... Time = 1.9 break happen Running... Time = 2 break happen Running... Time = 2.1 break happen Running... Time = 2.2 break happen Running... Time = 2.3 break happen Running... Time = 2.4 break happen Running... Time = 2.5 break happen Running... Time = 2.6 break happen Running... Time = 2.7 break happen Running... Time = 2.8 break happen Running... Time = 2.9 break happen Running... Time = 3 break happen break happen Running... Time = 3.1 break happen break happen Running... Time = 3.2 break happen break happen Running... Time = 3.3 break happen break happen Running... Time = 3.4 break happen break happen break happen Running... Time = 3.5 break happen break happen break happen Running... Time = 3.6 break happen break happen Running... Time = 3.7 break happen break happen Running... Time = 3.8 break happen break happen Running... Time = 3.9 break happen break happen Running... Time = 4 break happen break happen Running... Time = 4.1 break happen break happen Running... Time = 4.2 break happen break happen Running... Time = 4.3 break happen break happen Running... Time = 4.4 break happen break happen Running... Time = 4.5 break happen break happen Running... Time = 4.6 break happen break happen Running... Time = 4.7 break happen break happen break happen Running... Time = 4.8 break happen break happen break happen break happen Running... Time = 4.9 break happen break happen break happen break happen Running... Time = 5 break happen break happen break happen break happen break happen Running... Time = 5.1 break happen break happen break happen break happen Running... Time = 5.2 break happen break happen break happen break happen Running... Time = 5.3 break happen break happen break happen break happen Running... Time = 5.4 break happen break happen break happen break happen Running... Time = 5.5 break happen break happen break happen Running... Time = 5.6 break happen break happen break happen Running... Time = 5.7 break happen break happen break happen Running... Time = 5.8 break happen break happen break happen Running... Time = 5.9 break happen break happen break happen Running... Time = 6 break happen break happen break happen Running... Time = 6.1 break happen break happen break happen Running... Time = 6.2 break happen break happen break happen Running... Time = 6.3 break happen break happen break happen Running... Time = 6.4 break happen break happen break happen Running... Time = 6.5 break happen break happen break happen Running... Time = 6.6 break happen break happen break happen Running... Time = 6.7 break happen break happen Running... Time = 6.8 break happen Running... Time = 6.9 break happen Running... Time = 7 Running... Time = 7.1 Running... Time = 7.2 Running... Time = 7.3 Running... Time = 7.4 Running... Time = 7.5 Running... Time = 7.6 Running... Time = 7.7 Running... Time = 7.8 Running... Time = 7.9 Running... Time = 8 Running... Time = 8.1 Running... Time = 8.2 Running... Time = 8.3 Running... Time = 8.4 Running... Time = 8.5 Running... Time = 8.6 Running... Time = 8.7 Running... Time = 8.8 Running... Time = 8.9 Running... Time = 9 Running... Time = 9.1 Running... Time = 9.2 Running... Time = 9.3 Running... Time = 9.4 Running... Time = 9.5 Running... Time = 9.6 break happen break happen Running... Time = 9.7 break happen break happen Running... Time = 9.8 break happen break happen Running... Time = 9.9 break happen break happen Running... Time = 10 break happen break happen Running... Time = 10.1 break happen break happen Running... Time = 10.2 break happen break happen Running... Time = 10.3 break happen break happen Running... Time = 10.4 break happen break happen Running... Time = 10.5 break happen break happen Running... Time = 10.6 break happen break happen Running... Time = 10.7 break happen break happen Running... Time = 10.8 break happen break happen Running... Time = 10.9 break happen break happen Running... Time = 11 break happen break happen Running... Time = 11.1 break happen break happen Running... Time = 11.2 break happen break happen Running... Time = 11.3 break happen break happen Running... Time = 11.4 break happen break happen break happen Running... Time = 11.5 break happen break happen break happen Running... Time = 11.6 break happen Running... Time = 11.7 break happen break happen Running... Time = 11.8 break happen break happen Running... Time = 11.9 break happen break happen Running... Time = 12 break happen break happen Running... Time = 12.1 break happen break happen Running... Time = 12.2 break happen break happen Running... Time = 12.3 break happen break happen Running... Time = 12.4 break happen break happen break happen Running... Time = 12.5 break happen break happen break happen Running... Time = 12.6 break happen break happen break happen Running... Time = 12.7 break happen break happen break happen Running... Time = 12.8 break happen break happen break happen Running... Time = 12.9 break happen break happen break happen Running... Time = 13 break happen break happen break happen Running... Time = 13.1 break happen break happen break happen Running... Time = 13.2 break happen break happen break happen Running... Time = 13.3 break happen break happen break happen Running... Time = 13.4 break happen break happen break happen Running... Time = 13.5 break happen break happen break happen Running... Time = 13.6 break happen break happen break happen Running... Time = 13.7 break happen break happen Running... Time = 13.8 break happen break happen Running... Time = 13.9 break happen break happen Running... Time = 14 break happen break happen Running... Time = 14.1 break happen break happen Running... Time = 14.2 break happen break happen Running... Time = 14.3 break happen break happen Running... Time = 14.4 break happen Running... Time = 14.5 break happen break happen Running... Time = 14.6 break happen break happen Running... Time = 14.7 break happen break happen Running... Time = 14.8 break happen break happen break happen Running... Time = 14.9 break happen break happen break happen Running... Time = 15 break happen break happen break happen Running... Time = 15.1 break happen break happen break happen Running... Time = 15.2 break happen break happen break happen break happen Running... Time = 15.3 break happen break happen break happen break happen Running... Time = 15.4 break happen break happen break happen Running... Time = 15.5 break happen break happen break happen Running... Time = 15.6 break happen break happen break happen Running... Time = 15.7 break happen break happen break happen Running... Time = 15.8 break happen break happen break happen Running... Time = 15.9 break happen break happen break happen Running... Time = 16 break happen break happen break happen Running... Time = 16.1 break happen break happen break happen Running... Time = 16.2 break happen break happen break happen Running... Time = 16.3 break happen break happen break happen Running... Time = 16.4 break happen break happen break happen Running... Time = 16.5 break happen break happen Running... Time = 16.6 break happen break happen Running... Time = 16.7 break happen break happen Running... Time = 16.8 break happen Running... Time = 16.9 break happen Running... Time = 17 break happen Running... Time = 17.1 break happen Running... Time = 17.2 Running... Time = 17.3 Running... Time = 17.4 Running... Time = 17.5 break happen Running... Time = 17.6 break happen Running... Time = 17.7 break happen Running... Time = 17.8 break happen Running... Time = 17.9 break happen Running... Time = 18 break happen Running... Time = 18.1 break happen break happen Running... Time = 18.2 break happen break happen Running... Time = 18.3 break happen break happen Running... Time = 18.4 break happen break happen Running... Time = 18.5 break happen break happen Running... Time = 18.6 break happen break happen Running... Time = 18.7 break happen break happen Running... Time = 18.8 break happen break happen Running... Time = 18.9 break happen break happen Running... Time = 19 break happen break happen Running... Time = 19.1 break happen break happen Running... Time = 19.2 break happen break happen Running... Time = 19.3 break happen break happen Running... Time = 19.4 break happen Running... Time = 19.5 break happen Running... Time = 19.6 break happen Running... Time = 19.7 break happen Running... Time = 19.8 break happen Running... Time = 19.9 break happen