Often in John Ehler's various published code written in TradeStation EL, you will see A line like this to prevent division by zero.
From Hilbert Period and indicators that use it as part:
If Q1 <> 0 and Q1[1] <> 0 then Do Some division.....
It has always been a mystery to me what TradeStation would do in a case when Q1 is equal to zero, because Ehlers never gives code to do something different if the zero test above says we are about to divide by zero. There are never any lines for that situation. What would happen on that bar if Q1 was = 0? In a simple situation maybe nothing gets plotted for that bar,...
but what happens on the next bar with code the references the value [1] (one bar ago?
To make CoronaCycle plot so that it looks like Ehlers TradeStation plot I used:
if (denom == 0) //this step is not in other versions, but if we don't do this test, // The plot does not match the photo in Ehler's article { dc.Set(dc[1]); } if (denom != 0) { dc.Set(.5*num/denom); }
Today I was reading the code inside a community shared indicator of Ehler's called Smoothed Adaptive Momentum (SAM).
The author of this code did something that is probably the correct way to handle situations like this, where you must avoid dividing by zero, but I dont understand what it does. Would someone be so kind as to explain it to me?
The relevant section of code where the author is testing for and avoiding division by zero is:
if (!IsZero(Q1[0])&& !IsZero(Q1[1])) { DeltaPhase.Set((I1[0] / Q1[0] - I1[1]/Q1[1]) / (1 + I1[0] * I1[1]/(Q1[0] * Q1[1]))); } if (DeltaPhase[0] < 0.1) { DeltaPhase.Set(0.1); } else if (DeltaPhase[0] > 1.1) { DeltaPhase.Set(1.1); } double MedianDelta = CurrentBar > 4 ? GetMedian(DeltaPhase, 5) : 0.0; double DC = IsZero(MedianDelta) ? 15 : ((6.28318/MedianDelta) + 0.5);
private bool IsZero(double v) { return Math.Abs(v - v) < double.Epsilon; }
I would like to know what this double.Episilon is, and how it is used here.... I'm guessing it helps avoid division by zero, and gives some alternate handling incase we are about to divide by zero, but I can't figure out how it works.
Comment