# Problem with time comparision

 Register Blogs Members List Search Today's Posts Mark Forums Read

 December 3, 2016, 16:40 Problem with time comparision #1 Senior Member   Join Date: Jan 2015 Posts: 150 Rep Power: 11 I try to identify some specific moment during a simulation. That's why I got a current time value and tried to compare it with specific time moment, e.g. 0.0006. Unfortunately the comparison doesn't work, because when currentTime == 0.0006 and stopTime_ == 0.0006, the if statement evaluates condition as FALSE. However, I know that currentTime and stopTime are equal (according to output of Info object). The only condition which is working is "if (0.0006 == stopTime_)". If I use currentTime in condition statement, then FALSE is generated. It seems that there is some problem with currentTime variable. Yes, it has a type of scalar, which is typedef of double. Variable stopTime_ also has the same time. That's why I really don't know why condition with stopTime_ works fine, while using currentTime variable it fails. Here is a part of my code: Code: ``` const scalar currentTime = obr_.time().value(); scalar stopTime_; dict.lookup("stopTime") >> stopTime_; //if (currentTime == stopTime_) - fail //if (0.0006 == stopTime_) - OK if (0.0006 == currentTime) //- fail { //if == true } else { //if == false }```

 December 5, 2016, 07:43 #2 New Member   Paul Bomke Join Date: Mar 2010 Location: Bremen, Germany Posts: 16 Rep Power: 16 Hi Svensen, I think your problem is the comparison of two floating point numbers. They might both look like 0.0006 but may indeed be 0.0005999999 and 0.0006000000. That's why you get false. Subtract them and check if the results' absolute value is below a certain tolerance: Code: `if( abs(0.0005999999 - 0.000600000) < 0.000001 )` Best, Paul

 December 5, 2016, 13:14 #3 Senior Member   Join Date: Jan 2015 Posts: 150 Rep Power: 11 Yes, Paul. It works fine!

 Tags openfoam-dev, programming, source code