For instance in circuit simulation, typical voltages are around 1V so a value of 1e-6 would be a suitable voltage absolute tolerance. For the absTolVal, a value that is a factor of 1e6 to 1e8 smaller than the largest value in your domain is a good guide. In my experience (analog circuit simulation), a value between 1e-2 and 1e-4 is good for the relTol.Whilst you can templatize such a function, this makes choosing the default values of the tolerances more difficult.If you don't use max or min then there will be cases where almostEqual(a, b) and almostEqual(b, a) give different results.Return diffVal <= relTolVal || diffVal <= absTolVal bool almostEqual(double a, double b, double relTol=1e-3, double absTolVal=1e-8)ĭouble maxVal = std::max(std::fabs(a), std::fabs(b)) the amount of numerical error in your calculationsįinally, you may need comparison functions with a relative tolerance, and absolute tolerance or both.
0 Comments
Leave a Reply. |