|
[Sponsors] |
December 3, 2016, 17: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, 08: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 ) Paul |
|
December 5, 2016, 14:14 |
|
#3 |
Senior Member
Join Date: Jan 2015
Posts: 150
Rep Power: 11 |
Yes, Paul. It works fine!
|
|
Tags |
openfoam-dev, programming, source code |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Floating point exception error | lpz_michele | OpenFOAM Running, Solving & CFD | 53 | October 19, 2015 03:50 |
How to write k and epsilon before the abnormal end | xiuying | OpenFOAM Running, Solving & CFD | 8 | August 27, 2013 16:33 |
Problem with FloatingObject | Leech | OpenFOAM Running, Solving & CFD | 10 | March 29, 2012 16:24 |
IcoFoam parallel woes | msrinath80 | OpenFOAM Running, Solving & CFD | 9 | July 22, 2007 03:58 |
Could anybody help me see this error and give help | liugx212 | OpenFOAM Running, Solving & CFD | 3 | January 4, 2006 19:07 |