# Problem with time comparision

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

 December 3, 2016, 17:40 Problem with time comparision #1 Senior Member   Join Date: Jan 2015 Posts: 143 Rep Power: 4 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, 08:43 #2 New Member   Paul Bomke Join Date: Mar 2010 Location: Bremen, Germany Posts: 16 Rep Power: 9 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, 14:14 #3 Senior Member   Join Date: Jan 2015 Posts: 143 Rep Power: 4 Yes, Paul. It works fine!

 Tags openfoam-dev, programming, source code

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post lpz_michele OpenFOAM Running, Solving & CFD 53 October 19, 2015 02:50 xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33 Leech OpenFOAM Running, Solving & CFD 10 March 29, 2012 15:24 msrinath80 OpenFOAM Running, Solving & CFD 9 July 22, 2007 02:58 liugx212 OpenFOAM Running, Solving & CFD 3 January 4, 2006 19:07

All times are GMT -4. The time now is 14:37.